C# Feature β Anonymous types
Overview
Section titled βOverviewβAnonymous types create object instances without declaring a formal class, often used in LINQ projections.
Introduced In
Section titled βIntroduced InβC# 3 (2007)
class PersonView { public string Name { get; set; } public int Age { get; set; } }var person = new { Name = "Ada", Age = 42 };Annotated Example
Section titled βAnnotated Exampleβvar query = people.Select(p => new { p.Name, IsAdult = p.Age >= 18 });Gotchas & Best Practices
Section titled βGotchas & Best Practicesβ- Scope-limited; types are compiler-generated and local to the assembly.
- Great for projections; avoid returning anonymously typed values from public APIs.
Related Features
Section titled βRelated FeaturesβVersion Notes
Section titled βVersion Notesβ- Works seamlessly with
vartype inference.