Skip to content

C# Feature – Anonymous types

Anonymous types create object instances without declaring a formal class, often used in LINQ projections.

C# 3 (2007)

class PersonView { public string Name { get; set; } public int Age { get; set; } }
var person = new { Name = "Ada", Age = 42 };
var query = people.Select(p => new { p.Name, IsAdult = p.Age >= 18 });
  • Scope-limited; types are compiler-generated and local to the assembly.
  • Great for projections; avoid returning anonymously typed values from public APIs.
  • Works seamlessly with var type inference.