Records and Value Equality
Represent immutable data with concise record types.
Introduction
Records are ideal for data-centric models where equality should depend on values.
The with expression creates a copy with one changed property, leaving the original record unchanged.
Runnable C# example
Program.cs
using System;
record User(string Name, string Role);
class Program
{
static void Main()
{
var original = new User("Ada", "Developer");
var promoted = original with { Role = "Lead" };
Console.WriteLine(promoted);
}
}
User { Name = Ada, Role = Lead }Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Unique Records Remove duplicate records that are equal by value, keeping the first of each in order. Easy