// Compiler Infers Type จาก Right-hand Side
var name = "John"; // string
var age = 30; // int
var numbers = new List(); // List
var person = new Person(); // Person
var result = GetData(); // Return Type ของ GetData()
// ❌ Error: ไม่มี Initializer
// var x;
// ❌ Error: Null ไม่มี Type Info
// var x = null;
// ✅ OK: Explicit Cast
var x = (string)null;
class Program
{
static void Main()
{
// VAR - Static Typing
var list = new List { "A", "B" };
list.Add("C"); // ✅ IntelliSense OK
// list.Add(123); // ❌ Compile Error: int → string
// DYNAMIC - Dynamic Typing
dynamic dynList = new List { "A", "B" };
dynList.Add("C"); // ✅ Runtime OK
dynList.Add(123); // ✅ Compile OK, Runtime OK (List
เมื่อไหร่ใช้ var
✅ Default Choice สำหรับ Local Variables
✅ Type ชัดเจนจาก RHS (Constructor, Factory, Cast)
✅ Anonymous Types: var anon = new { Name = "John", Age = 30 };
✅ LINQ Queries: var query = from x in list where x > 5 select x.Name;
✅ Complex Generic Types: var dict = new Dictionary>();
เมื่อไหร่ใช้ dynamic
✅ COM Interop (Office, Legacy ActiveX)
✅ Dynamic Languages (IronPython, JavaScript)
✅ JSON/XML Ad-hoc Parsing (JObject, XElement)
✅ Plugin/Scripting Systems
✅ Reflection-heavy Code Simplification
เมื่อไหร่ ไม่ควร ใช้ dynamic
❌ General Purpose Variables
❌ Public API / Method Parameters / Return Types
❌ Performance Critical Paths
❌ Domain Models / DTOs / Entities
❌ ที่มี Strong Type Alternative (Records, JsonSerializer, Source Generators)
Modern Alternatives to dynamic
// 1. System.Text.Json.Nodes (DOM API)
JsonNode node = JsonNode.Parse(json);
string name = node["name"]!.GetValue();
// 2. JsonSerializer + Anonymous Type (Deserialize to Shape)
var shape = new { Name = "", Age = 0 };
var data = JsonSerializer.Deserialize(json, shape.GetType());
// 3. Source Generators (Compile-time JSON Types)
// 4. Records + Pattern Matching
if (person is { Name: "Admin", Age: > 18 }) { ... }