Search⌘ K

Anonymous Types

Explore the concept of anonymous types in C# and how to create them using the var keyword and object initializers. Understand their read-only properties and how identical anonymous types share the same structure. Discover projection initializers for efficient data selection, often used with LINQ to manage filtered datasets without extra classes.

Overview

C# supports anonymous types in addition to methods.

Anonymous types are like regular classes, but they don’t have user-defined names. We use the var keyword, along with the object initializer, to create an anonymous type:

var person = new { Name = "John", Age = 27 };

Now, the person variable contains a ...