Anonymous Types
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.
You create anonymous types by using the new
operator together with an object initializer. For more information about object initializers, see Object and Collection Initializers.
The following example shows an anonymous type that is initialized with two properties named Amount
and Message
.C#Copy
Anonymous types typically are used in the select
clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see LINQ in C#.
Last updated