C# Tutorial

Records

Immutability and value-based equality for data structures

What is a Record?

Introduced in C# 9.0, a Record is a special type of class or struct that provides built-in functionality for encapsulating data. Records are primarily designed for Immutability and Value-based equality.
csharp
public record User(string Name, int Age);
Wait! With just one line, C# generates:
  1. A constructor.
  2. Properties for Name and Age.
  3. Value-based equality logic (if two records have the same data, they are equal).
  4. A clean ToString() method.

Immutability by Default

Records often use init properties, which mean they can only be set during initialization and never changed again.
csharp
var user = new User("Alpha", 25);
// user.Age = 26; // ERROR! Records are immutable.

Non-Destructive Mutation (with)

Wait! If records are immutable, how do you "change" them? You use the with expression to create a new copy of the record with some modified values.
csharp
var olderUser = user with { Age = 26 };

Equality: Value vs. Reference

  • Classes: Two instances are equal only if they point to the same memory location.
  • Records: Two instances are equal if all their properties match.
csharp
var u1 = new User("A", 1);
var u2 = new User("A", 1);
Console.WriteLine(u1 == u2); // TRUE for Records!

Summary

FeatureDescription
recordKeyword to define a data-centric type
initProperties that are read-only after creation
withCreating a modified copy of an object
DeconstructBreaking a record apart into separate variables
PositionalShort syntax: record Person(string Name)
Best ForData Transfer Objects (DTOs), API models, and Redux-like state
Logic"Data is more important than behavior"
SafetyPrevents accidental state changes (Bugs)
ModernThe "Functional" way to handle data in C#
IdentityBased on content, not memory address
InheritanceRecords can inherit from other records
StructYou can also define record struct for value-type records
RefactorReplacing boilerplate classes with records reduces code by ~90%