匿名与动态

匿名类型允许创建对象,而无需提前显式定义其类型,同时保持静态类型检查。

var anon = new { Value = 1 };
Console.WriteLine(anon.Id); // compile time error

相反,dynamic 具有动态类型检查,选择运行时错误,而不是编译时错误。

dynamic val = "foo";
Console.WriteLine(val.Id); // compiles, but throws runtime error