C# Anonymous Type vs Record: Practical Comparison
Compare C# anonymous types and records on syntax, equality, immutability, and when to choose each for data modeling.

When you want to group a few values with less boilerplate than a traditional class, C# offers two concise options: anonymous types and records. Both provide value-based equality, but they serve different purposes. Understanding how each behaves at compile time and runtime is what helps you choose between them.
Syntax and Declaration Differences
The most immediate difference is how you declare them. An anonymous type uses the new keyword with an object initializer and no type name:
var person = new { Name = "Alice", Age = 30 };The compiler infers the type, and you usually refer to it through var. You cannot write a method that accepts an anonymous type by name, nor can you return one from a method without resorting to object, dynamic, or a generic helper.
A record, on the other hand, is a named type. You declare it explicitly:
public record Person(string Name, int Age);You can then use Person as a parameter, return type, or field. This simple naming difference has far-reaching consequences for how the type integrates with the rest of your codebase.
Type Identity and Naming
Anonymous types are compiler-generated. Each distinct set of property names and types produces a separate internal type, and two anonymous types with the same shape are the same type only within the same assembly. Across an assembly boundary, the compiler-generated type is not accessible by name, so the anonymous object is usually handled as object, dynamic, or a generic T.
Records are named types. They can be declared wherever you normally declare a type, and they appear in IntelliSense and documentation. You can use them in method signatures, generic constraints, and pattern matching. This makes records suitable for public APIs and for data that must be exchanged between modules.
Equality Semantics
Both anonymous types and records implement value-based equality, but the implementation differs. Anonymous types override Equals and GetHashCode based on the values of their properties. Two anonymous type instances with the same property values are equal, but only if they are the same compiler-generated type.
Records provide a more robust equality model. They implement IEquatable<T>, and the compiler generates Equals, GetHashCode, and the == and != operators. Records also support with expressions for non-destructive mutation, which is useful when you need a copy with one field changed:
var olderPerson = person with { Age = 31 };This is not possible with anonymous types. If you need to modify a value, you must create a new anonymous type manually.
Mutability and Immutability
Anonymous types are always immutable. Their properties are read-only and can only be set during initialization.
Records declared with positional parameters are immutable by default: their properties use init accessors rather than set. If you need mutable properties, define them explicitly with set accessors in the record body. For example:
public record MutablePerson
{
public string Name { get; set; }
public int Age { get; set; }
}The default record behavior is designed to encourage immutability, but the language gives you flexibility. Anonymous types offer no such choice; they are immutable by design.
Use Cases: When to Choose Each
Anonymous types shine in LINQ queries and local projections. When you only need a shape to carry data from one part of a method to another, they reduce boilerplate. For example:
var result = people
.Where(p => p.Age > 18)
.Select(p => new { p.Name, p.Age });Here, the anonymous type is a convenient container for the projected fields. You never need to name it, and it disappears after the method scope.
Records are better when the data structure has a longer lifespan or crosses boundaries. Use records for DTOs, domain events, API responses, or any data that needs to be compared, serialized, or passed between layers. Records also integrate with System.Text.Json and other serializers without extra configuration, whereas anonymous types are less suitable for deserialization: the compiler-generated type is internal and has no public parameterless constructor.
Performance and Runtime Considerations
Both anonymous types and records are reference types (unless you use a record struct). The runtime cost of allocation is similar. The main performance difference comes from equality. Anonymous types compare each property when Equals is called, but they do not implement IEquatable<T>. Records generate a more optimized equality implementation using EqualityComparer<T>.Default and also support IEquatable<T> to avoid boxing. For most scenarios, the difference is negligible.
Records also support record struct for value-type semantics, which can reduce heap allocations in hot paths. Anonymous types are always reference types. If you need a lightweight immutable value type, a record struct is a better choice than an anonymous type.
Compatibility and Maintainability
Anonymous types are confined to the assembly in which they are declared. Their compiler-generated names are not intended for source code, which makes them awkward for reflection-based frameworks that need a stable named type. Records, being named types, are easy to document, test, and refactor. You can add attributes, implement interfaces, and extend them with additional members.
Maintainability also improves with records because they are explicit. When you see a record declaration, you know exactly what properties exist and what behavior is inherited. Anonymous types hide their shape, which can make code harder to understand when the projection is complex or reused in multiple places.
| Feature | Anonymous Type | Record |
|---|---|---|
| Named type | No (compiler-generated) | Yes |
| Immutable by default | Yes | Yes (with positional syntax) |
with expression | No | Yes |
| Cross-assembly usage | No | Yes |
| Serialization support | Limited | Good (with System.Text.Json) |
| Can be named in method signatures | No | Yes |
Making the Choice
The decision between an anonymous type and a record comes down to scope and intent. If the data is temporary, local to a method, and only used for a quick projection, an anonymous type is sufficient. If the data represents a meaningful concept in your domain, will be passed between methods or assemblies, or needs to support equality and mutation in a controlled way, a record is the better fit.
In practice, records are the default choice for any data shape that outlives a single expression. When you are designing a public API or a data model that will be shared, records give you the compile-time safety and discoverability that anonymous types cannot provide. For one-off transformations inside a query, anonymous types keep the code concise without adding a named type to your project.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.