Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without defining a named type first. The compiler generates a type name at compile time that you can't access in your source code. The compiler infers the type of each property.
Create anonymous types by using the new operator together with an object initializer.
Because the compiler generates the type name and you can't access it in source code, you must use var to declare the local variable. You can't specify the type name explicitly.
Two anonymous type instances that have the same property names and types in the same order share the same compiler-generated type.
Anonymous types can contain other anonymous types as property values.
- The compiler generates them as
internal sealed classtypes that derive fromObject. - All properties are
publicand read-only. - Anonymous types support
withexpressions for nondestructive mutation. - The compiler generates value-based
Equals,GetHashCode, andToStringoverrides. - Anonymous types support expression trees, while tuples don't.
- You can't use them as method return types, method parameters, or field types because you can't name the type.
- They're scoped to the method where you declare them.
- You can't add methods, events, or custom operators.
- Properties are always read-only; anonymous types don't support mutable properties.
For most new code, consider using tuples instead of anonymous types. As value types, tuples provide better performance. They also provide deconstruction support and more flexible syntax. Anonymous types remain the better choice when you need expression tree support or reference-type semantics.