software master at the intersection of technology, science and art



anonymous types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler. The following example shows an anonymous type being initialized with two properties called Amount and Message.

var v = new { Amount = 108, Message = "Hello" };

This is used extensively in passing html attributes in MVC applications and in Linq

Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. An anonymous type cannot be cast to any interface or type except for object. They are also used to initialize fields in a constructor

var profile = new UserProfile() { UserProfileID =1, FirstName = "Brenda", LastName = "Jones}"