Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

Anonymous types

Reference for C# anonymous types: new { } syntax, inferred property names, immutability, equality, with expressions, LINQ projections and their limits.

An anonymous type is a small class the compiler creates for you from an object initializer without a type name. You write new { Name = "Ada", Age = 36 } and get an object with read-only properties Name and Age. You cannot write the type's name, so you hold it in a var.

Anonymous types shine in LINQ queries that reshape data for a moment, such as projecting a few columns, grouping, or joining. When the shape must leave the method (a return value, a field, a parameter), use a record or a tuple instead.

C#
var person = new { Name = "Ada", Age = 36 };
Console.WriteLine(person.Name);

Try it

C# Creating and projecting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Equality, with and limits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
var t = new { Property1 = value1, Property2 = value2 };
var u = new { variable, obj.Member };          // names inferred
Rule Detail
Members Only public read-only properties; no methods, events or fields.
Property names Taken from the left side, or inferred from a simple name or member access. Two properties with the same name are an error.
Type identity Two initializers with the same property names, types and order in the same assembly produce the same type.
Base type Always object; the class is sealed.
Generated members Equals, GetHashCode and ToString (format { X = 1, Y = 2 }).

Errors. Assigning to a property is a compile error, and null alone cannot initialize a member without a cast because its type cannot be inferred.

Usage notes

  • Value-like equality via Equals, but == compares references. Use Equals or compare the fields.
  • with works on anonymous types and copies the object with the listed members changed.
  • Scope. The type cannot appear in a signature. Passing one out of a method requires object or dynamic, which loses compile-time checking. Prefer a record or a tuple for anything that crosses a method boundary.
  • Tuples vs anonymous types: tuples are value types with positional (or named) elements and can be returned; anonymous types are reference types with properties and are expression-tree friendly.
  • Expression trees. Anonymous types can be used in query providers that translate LINQ to SQL or other languages, which is one reason they persist even though tuples exist.
  • Reflection sees an ordinary class with a compiler-generated name, so serializers handle it like any other object.
  • Lifetime. Each distinct shape adds a generated class to the assembly, which is harmless in normal code.