Anonymous Types
Learn how to create objects on the fly without defining explicit classes using anonymous types, and understand their immutability and equality behaviors.
C# allows us to encapsulate a set of read-only properties into a single object without explicitly defining a class. These are known as anonymous types.
An anonymous type is a temporary class generated by the compiler. Because the generated class has no name we can reference, we must use the var keyword to hold the instance. We define properties using object initializer syntax.
Creating and using anonymous types
Let’s look at how to create a simple anonymous object.