100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

C# Records & Pattern Matching Cheat Sheet

C# Records & Pattern Matching Cheat Sheet

Explains C# records for immutable value-based types, the with expression, and modern pattern matching including property and positional patterns.

2 PagesAdvancedMar 22, 2026

Records

Immutable, value-equal reference and value types.

csharp
public record Point(int X, int Y);              // Positional record, immutable by defaultvar p1 = new Point(1, 2);var p2 = new Point(1, 2);Console.WriteLine(p1 == p2);                     // True: value-based equalityvar p3 = p1 with { Y = 5 };                      // Non-destructive mutation: copies p1, changes YConsole.WriteLine(p3);                           // Point { X = 1, Y = 5 } (auto ToString)public record class Person(string Name, int Age); // record class (reference type, the default)public record struct Coord(double Lat, double Lng); // record struct (value type)

Pattern Matching

Switch expressions with type, relational, and logical patterns.

csharp
object value = 42;string description = value switch{    int n when n < 0 => "negative",    0 => "zero",    int n and > 0 and < 10 => "small positive",   // Relational + logical patterns    int => "large integer",    string s => $"a string: {s}",    null => "nothing",    _ => "unknown"};if (value is int number && number > 10)           // Type pattern + condition{    Console.WriteLine($"Big number: {number}");}

Deconstruction & Record Patterns

Pulling positional and property values out of a record.

csharp
var point = new Point(3, 4);var (x, y) = point;                     // Deconstruction (auto-generated for records)string quadrant = point switch{    Point { X: > 0, Y: > 0 } => "Q1",   // Property pattern matching on record members    Point { X: < 0, Y: > 0 } => "Q2",    Point(0, 0) => "Origin",             // Positional record pattern    _ => "Other"};

Key Concepts

What makes records and pattern matching distinct.

  • Value equality- Records override Equals/GetHashCode to compare by value, not by reference
  • with expression- Creates a shallow copy with specified properties changed (non-destructive mutation)
  • record struct- A C# 10+ value-type record; avoids heap allocation for small immutable data
  • init accessors- public int X { get; init; } allows setting a property only during object initialization
  • Switch expressions- x switch { ... } is an expression form of switch that must be exhaustive or have a _ arm
  • Pattern kinds- constant, type, relational (>, <), logical (and/or/not), property, positional, and list patterns
Pro Tip

Use record struct for small, frequently created immutable value types like coordinates or money amounts to get value semantics without the heap allocation and GC pressure of a record class.

Was this cheat sheet helpful?

Explore Topics

#CRecordsPatternMatching#CRecordsPatternMatchingCheatSheet#Programming#Advanced#Records#PatternMatching#DeconstructionRecordPatterns#KeyConcepts#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet