Anonymous type and dynamic type

What are the real differences between the anonymous type (var) in C # 3.0 and the dynamic type (dynamic) that comes in C # 4.0?

+31
c # dynamic anonymous-types
Dec 24 '08 at 14:28
source share
6 answers

An anonymous type is a real compiler-generated type created for you. It is good that the compiler can reuse this type later for other operations that require it, since it is POCO.

My understanding of dynamic types is that they are late, which means the CLR (or DLR) will evaluate the object at runtime, and then use duck typing to allow or deny members access to the object.

So, I think the difference is that anonymous types are true POCOs that the compiler can see, but you can use them, and dynamic types are related to late dynamic objects.

+16
Dec 24 '08 at 14:34
source share

It seems you are mixing three completely different orthogonal things:

  • static and dynamic typing
  • manifest against implicit typing
  • named against anonymous types

These three aspects are completely independent, they have nothing to do with each other.

Static or dynamic typing refers to when type checking occurs: dynamic typing is performed at run time, static typing is performed before execution.

The manifesto against implicit typing refers to whether types are explicit in the source code or not: manifest typing means that the programmer must write the types to the source code, implicit typing means that the type system computes them on its own.

Named or anonymous types refer to whether the types have names or not.

The dynamic keyword in C # 4.0 means that this variable, parameter, method, field, property ... no matter what is dynamically typed, i.e. that its type will be checked at runtime. Everything that is not typed as dynamic is statically typed. Whether a type is static or dynamic, not only determines when type checking occurs, but in C # 4.0, it also determines when a method is dispatched. In C #, method dispatch is performed at runtime based on a static type (except for polymorphism of runtime subtypes), while on dynamically typed objects in C # 4.0, method dispatch is performed at runtime based on the type of runtime.

The var keyword in C # 3.0 means that this local variable will be implicitly entered, i.e. that instead of the programmer writing the type explicitly, the type system will decide on its own. This has nothing to do with dynamic typing, at least in C # 3.0. The variable will be strongly statically entered as if you yourself wrote the type. This is just convenience: for example, why do you need to write all type names twice in HashMap<int, string> foo = new HashMap<int, string>(); when the type system can clearly determine that foo is HashMap<int, string> , so instead you write var foo = new HashMap<int, string(); Please note that there is nothing dynamic or anonymous in this. The type is static and has a name: HashMap<int, string> . Of course, in C # 4.0, if the type system determines that the right side of the destination is dynamic, the type of the variable on the left will be dynamic.

An anonymous type in C # 3.0 means that this type has no name. Well, in fact, real anonymous types would require a back-incompatible change in the Common Type System, so what really happens behind the curtain is that the compiler will generate a very long, very random, unique and illegal name for the type and put is the name where the anonymous type appears. But from the point of view of the programmer, the type has no name. Why is this useful? Well, sometimes you have intermediate results that you only need in brief, and then throw them away again. Providing such transitional types alone could raise them to a level of importance that they simply do not deserve. But then again, there is nothing dynamic about it.

So, if a type does not have a name, how can a programmer access it? Well, she can't! At least not directly. What a programmer can do is describe the type: it has two properties, one of which is called a "name" of type string , the other is called "id" of type int . This is the type I want, but I don’t care what he called.

Here the pieces begin to come together. In C #, you must declare types of local variables by explicitly writing type names. But how can you spell a name of a type that has no name? This is where var occurs: since with C # 3.0 this is actually no longer true: you no longer need to write down the names, you can also tell the compiler to figure this out. So, although what I wrote in the first paragraph above is true that implicit typings and anonymous types have nothing to do with others, it is also true that anonymous types would be useless without implicit typing.

Note, however, that the opposite is not true: implicit typing is completely useful without anonymous types. var foo = HashMap<int, string> makes sense and there is no anonymous type in the field of view.

+75
Dec 24 '08 at 20:14
source share

The dynamic type is essentially an object , but will allow all calls to the method / property / operator, etc. at run time via DLR or another provider (e.g. reflection).

This makes it very similar to VB with Option Strict Off and makes it very versatile for calling in COM or in DLR types.

No type checking at compile time with dynamic; conovedely, anonymous types are regular static typed, type-tested animals (you can see them in the reflector, although they are not very good).

In addition, anonymous types can only be processed by the compiler; dynamic requires extensive runtime support, so anonymous types are a C # function, but dynamic will be pretty much implemented by .NET 4.0 (with some C # 4.0 support).

+15
Dec 24 '08 at 14:34
source share

Check out the Ander presentation here:

http://channel9.msdn.com/pdc2008/TL16/

NTM

+8
Dec 24 '08 at 14:31
source share

There three times, with three actors - one at a time.

  • Development Time - Programmer
  • Compiler compile-time - C #
  • Runtime runtime

Anonymous types are declared called compiler. This expression is based on the programmer's specification (how he used the type). Since these types are called after the programmer left the process, they appear to be nameless to the programmer, therefore, they are "anonymous."

  • The programmer says: some type has a name and address
  • The compiler says: there is a type called xyz with the names and addresses Name and Address, both lines.
  • runtime says: I can't tell any difference between xyz and any type that the programmer made.

dynamic insertion in C # allows you to call methods that may or may not exist at compile time. This is useful for calling in python or javascript that are not compiled.

  • The programmer says: consider this instance of the car as dynamic. Now, the quack.
  • The compiler says: dynamic typing eh? should be good. I will not complain because I cannot verify this.
  • The runtime is trying to instantiate the car, quack.
+5
Dec 24 '08 at 18:26
source share

Nothing like a little code to clarify the situation:

 // anonymous types var anonType = new {Id = "123123123", Name = "Goku", Age = 30, DateAdded = new DateTime()}; // notice we have a strongly typed anonymous class we can access the properties with Console.WriteLine($"Anonymous Type: {anonType.Id} {anonType.Name} {anonType.Age} {anonType.DateAdded}"); // compile time error //anonType = 100; // dynamic types dynamic dynType = 100.01m; Console.WriteLine($"Dynamic type: {dynType}"); // it ok to change the type however you want dynType = new List<DateTime>(); Console.WriteLine($"Dynamic type: {dynType}"); // mix dynamic and anonymous dynamic dynamicAnonymousType = new {Id = 8000, FirstName = "Goku", Gender = "male", IsSuperSaiyan = true}; // Wasn't sure this would work but it does! However, you lose intellisense on the FirstName so you have to type it manually. Console.WriteLine($"FirstName: {dynamicAnonymousType.FirstName}"); dynamicAnonymousType = 100; Console.WriteLine(dynamicAnonymousType); // runtime error Console.WriteLine($"Id: {dynamicAnonymousType.FirstName}"); 
0
Feb 23 '17 at 22:19
source share



All Articles