Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readC#
#C##anonymous types#records#data modeling
Illustration comparing C# anonymous type and record data structures with a balance scale and code blocks.

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:

csharp
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:

csharp
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:

csharp
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:

csharp
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:

csharp
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.

FeatureAnonymous TypeRecord
Named typeNo (compiler-generated)Yes
Immutable by defaultYesYes (with positional syntax)
with expressionNoYes
Cross-assembly usageNoYes
Serialization supportLimitedGood (with System.Text.Json)
Can be named in method signaturesNoYes

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 tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.