#839 – Anonymous Type Limitations
May 8, 2013 3 Comments
An anonymous type is a temporary data type that is inferred based on the data that you include in an object initializer.
An anonymous type has a couple of limitations. Its only members are public, read-only properties. The type can’t contain methods or other type members and you cannot write to its properties.
var tt = new { Name = "Kirby", Age = 12 }; tt.Name = "bob"; // Error - Name can't be assigned to
The anonymous type must be implicitly convertible to the type of a variable used to store the new object. In other words, the variable must either be implicitly typed using the var keyword, or must be of type object. You’ll almost always want to use the first option, since the resulting variable is still strongly typed.
Pingback: Dew Drop – May 8, 2013 (#1,543) | Alvin Ashcraft's Morning Dew
Pingback: Interesting .NET Links - May 8 , 2013 | TechBlog
Pingback: Daily Six Pack For May 9, 2013 - Get Your Tech Fix