Anonymous Type - ablealias/asp.net GitHub Wiki
Anonymous type, as the name suggests, is a type that doesn't have any name. C# allows you to create an object with the new keyword without defining its class. The implicitly typed variable- var
is used to hold the reference of anonymous types.
var myAnonymousType = new { firstProperty = "First",
secondProperty = 2,
thirdProperty = true
};
In the above example, myAnonymousType is an object of anonymous type created using the new keyword and object initializer syntax It includes three properties of different data types.
An anonymous type is a temporary data type that is inferred based on the data that you include in an object initializer. Properties of anonymous types will be read-only properties so you cannot change their values.
Scope of Anonymous Type
An anonymous type will always be local to the method where it is defined. Usually, you cannot pass an anonymous type to another method; however, you can pass it to a method that accepts a parameter of dynamic type. Please note that Passing anonymous types using dynamic is not recommended.