Anonymous Type vs Dynamic Type

前端 未结 6 532
春和景丽
春和景丽 2020-11-30 21:30

What are the real differences between anonymous type(var) in c# 3.0 and dynamic type(dynamic) that is coming in c# 4.0?

6条回答
  •  [愿得一人]
    2020-11-30 22:06

    The dynamic type is essentially object, but will resolve all method / property / operator etc calls at runtime via the DLR or other provider (such as reflection).

    This makes it much like VB with Option Strict Off, and makes it very versatile for calling into COM, or into DLR types.

    There is no type checking at compile time with dynamic; convesely, anonymous types are proper static-typed, type-checked beasts (you can see them in reflector, although they aren't pretty).

    Additionally, anonymous types can be handled exclusively by the compiler; dynamic requires extensive runtime support - so anonymous types are a C# feature, but dynamic will largely be implemented by .NET 4.0 (with some C# 4.0 support).

提交回复
热议问题