Search⌘ K
AI Features

Records

Explore the concept of records in C# to handle immutable data structures efficiently. Understand positional syntax for concise record definitions, value-based equality that compares data rather than references, and non-destructive mutation using the with expression. This lesson helps you reduce boilerplate and write safer, clearer code in data-centric applications.

C# applications frequently use data structures to transport data without relying on complex behavior. Developers can use classes or structs for this task, but C# provides the record type specifically for this purpose.

A record is a reference type that provides built-in functionality for encapsulating data. Unlike standard classes, records are designed to be immutable and focus on value-based equality rather than reference equality.

Positional syntax

Positional syntax is the most common way to define a record. This syntax declares the properties and the primary constructor on a single line.

C# 14.0
namespace DataModels;
// A record defined using positional syntax
public record Person(string FirstName, string LastName);
  • Line ...