#839 – Anonymous Type Limitations

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.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #839 – Anonymous Type Limitations

  1. Pingback: Dew Drop – May 8, 2013 (#1,543) | Alvin Ashcraft's Morning Dew

  2. Pingback: Interesting .NET Links - May 8 , 2013 | TechBlog

  3. Pingback: Daily Six Pack For May 9, 2013 - Get Your Tech Fix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: