[ACCEPTED]-Anonymous Type vs Dynamic Type-anonymous-types

Accepted answer
Score: 104

You seem to be mixing three completely different, orthogonal 83 things:

  • static vs. dynamic typing
  • manifest vs. implicit typing
  • named vs. anonymous types

Those three aspects are completely 82 independent, they have nothing whatsoever 81 to do with each other.

Static vs. dynamic typing refers to 80 when the type checking takes place: dynamic 79 typing takes place at runtime, static typing takes 78 place before runtime.

Manifest vs. implicit typing refers to whether the types 77 are manifest in the source code or not: manifest typing means that 76 the programmer has to write the types into the source 75 code, implicit typing means that the type system figures 74 them out on its own.

Named vs. anonymous types refers to, well, whether 73 the types have names or not.

The dynamic keyword 72 in C# 4.0 means that this variable, parameter, method, field, property 71 ... whatever is dynamically typed, i.e. that its type will 70 be checked at runtime. Everything that is 69 not typed as dynamic is statically typed. Whether 68 a type is static or dynamic not only determines 67 when type checking takes place, but in C# 4.0 66 it also determines, when method dispatch takes place. In 65 C#, method dispatch is done before runtime, based 64 on the static type (with the exception of 63 runtime subtype polymorphism of course), whereas 62 on dynamically typed objects in C# 4.0, method 61 dispatch is done at runtime, based on the 60 runtime type.

The var keyword in C# 3.0 means 59 that this local variable will be implicitly typed, i.e. that 58 instead of the programmer writing down the 57 type explicitly, the type system will figure 56 it out on its own. This has nothing to do 55 with dynamic typing, at least in C# 3.0. The 54 variable will be strongly statically typed 53 just as if you had written down the type 52 yourself. It is merely a convenience: for 51 example, why would you have to write down 50 all the type names twice in HashMap<int, string> foo = new HashMap<int, string>(); when the type system 49 can clearly figure out that foo is a HashMap<int, string>, so instead 48 you write var foo = new HashMap<int, string();. Please note that there is nothing 47 dynamic or anonymous about this. The type 46 is static and it has a name: HashMap<int, string>. Of course, in 45 C# 4.0, if the type system figures out that 44 the right hand side of the assignment is 43 dynamic, then the type of the variable on 42 the left hand side will be dynamic.

An anonymous type in 41 C# 3.0 means that this type has no name. Well, actually, real anonymous 40 types would have required a backwards-incompatible 39 change to the Common Type System, so what 38 actually happens behind the curtain is that the 37 compiler will generate a very long, very 36 random, unique and illegal name for the 35 type and put that name in wherever the anonymous 34 type appears. But from the programmer's 33 point of view, the type has no name. Why 32 is this useful? Well, sometimes you have 31 intermediate results that you only need 30 briefly and then throw away again. Giving 29 such transient types a name of their own 28 would elevate them to a level of importance 27 that they simply don't deserve. But again, there 26 is nothing dynamic about this.

So, if the 25 type has no name, how can the programmer 24 refer to it? Well, she can't! At least not 23 directly. What the programmer can do, is describe 22 the type: it has two properties, one called 21 "name" of type string, the other called "id" of 20 type int. That's the type I want, but I don't 19 care what it's called.

Here is where the 18 pieces start to come together. In C#, you 17 have to declare the types of local variables 16 by explicitly writing down the names of 15 the types. But, how can you write down the 14 name of a type that has no name? This is 13 where var comes in: because since C# 3.0, this 12 is actually no longer true: you no longer 11 have to write down the names, you can also 10 tell the compiler to figure it out. So, while 9 what I wrote in the first paragraph above 8 is true, that implicit typing and anonymous 7 types don't have anything to do with other, it 6 is also true that anonymous types would 5 be pretty useless without implicit typing.

Note, however, that 4 the opposite is not true: implicit typing 3 is perfectly useful without anonymous types. var foo = HashMap<int, string> makes 2 perfect sense and there's no anonymous type 1 in sight.

Score: 22

An anonymous type is a real, compiler-generated 13 type that is created for you. The good 12 thing about this is that the compiler can 11 re-use this type later for other operations 10 that require it as it is a POCO.

My understanding 9 of dynamic types is that they are late-bound, meaning 8 that the CLR (or DLR) will evaluate the 7 object at execution time and then use duck 6 typing to allow or disallow member access 5 to the object.

So I guess the difference 4 is that anonymous types are true POCOs that 3 the compiler can see but you can only use 2 and dynamic types are late-bound dynamic 1 objects.

Score: 20

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

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

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

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

Score: 7

There's three times, with three actors - one 11 in each time.

  • Design-time - programmer
  • Compile-time - c# compiler
  • Run-time - .net runtime

Anonymous types are declared 10 and named by the compiler. This declaration 9 is based on the programmer's specification 8 (how he used the type). Since these types 7 are named after the programmer has left 6 the process, they appear to be nameless 5 to the programmer, hence "anonymous".

  • The programmer says: Some type has a Name and Address
  • The compiler says : There's a type named xyz with Name and Address properties and fields, both strings.
  • the runtime says : I can't tell any difference between xyz and any type that the programmer made.

dynamic 4 typing in c# allows you to call methods 3 that may or may not exist at compile time. This 2 is useful for calling into python or javascript, which 1 are not compiled.

  • The programmer says: treat this instance of a car as a dynamic type. Now, quack.
  • The compiler says: dynamic typing eh? must be ok. I won't complain because I can't check it.
  • The runtime attempts to make the instance of car, quack.
Score: 3

Nothing like a little code to clear things 1 up:

// 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's 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;
// runtime error
Console.WriteLine($"Id: {dynamicAnonymousType.FirstName}");

More Related questions