#654 – You Can’t Use an Anonymous Type Directly

An anonymous type is a temporary data type that is inferred based on the data that you include in an object initializer.  For example, the code below causes a class to be generated that contains three properties–Title, Year and Director.

var movie = new { Title = "Gladiator", Year=2000, Director = "Ridley Scott" };

When you declare an object with an anonymous type like this, the compiler automatically generates the type for you.  You can use ILDASM to look at the IL for this code and see the type.  It has a fairly cryptic name.

While the compiler creates a type to represent the object that you declared, you can’t reference this type explicitly in your code.

Advertisements