Another new addition to C# is anonymous types. These are read-only objects whose type is created by the compiler rather than being defined in code. They are created using syntax very similar to object initializer syntax.
For example, the line:
var name = new { First="Jack", Last="Sprat" };
Creates an anonymous object with properties First
and Last
and assigns it to the variable name. Note we have to use var
, because the object does not have a defined type. Anonymous types are primarily used with LINQ, which we’ll cover in the future.