Complete Guide to Records in C# – With Examples | .NET Core

Complete Guide to Records in C# – With Examples | .NET Core

Records were introduced in C# 9.0 as a special kind of reference type designed to make it easier to work with immutable data and provide built-in value-based equality. This guide explains everything you need to know about records in C#, with practical examples.

What is a Record?

A record is a reference type like a class, but it compares instances by value rather than by reference. It’s ideal for scenarios like DTOs, configuration models, or read-only data structures.

Types of Records

  • Positional Record: Concise syntax with built-in constructor and deconstruct method.
  • Regular Record: Looks like a class with properties but benefits from value equality and immutability.

Positional Record Example

public record Person(string FirstName, string LastName);

var p1 = new Person("John", "Doe");
var p2 = new Person("John", "Doe");
Console.WriteLine(p1 == p2); // True

Regular Record Example

public record Car
{
    public string Brand { get; init; }
    public string Model { get; init; }
}

var car1 = new Car { Brand = "Toyota", Model = "Corolla" };

Key Features of Records

  • ✅ Value-based equality (compares data, not memory)
  • ✅ Built-in with expression for cloning
  • ✅ Support for deconstruction
  • ✅ Immutability using init
  • ✅ Great for APIs, DTOs, ViewModels

Understanding init vs set

init allows assignment only during object creation, ensuring immutability after initialization.

public class Person
{
    public string FirstName { get; init; }
}

var p = new Person { FirstName = "John" };
p.FirstName = "Mark"; // ❌ Error

Using with Expression

var car2 = car1 with { Model = "Camry" };

Deconstruction in Records

Positional records support automatic deconstruction:

var (first, last) = new Person("John", "Doe");

For regular records or classes, define manually:

public void Deconstruct(out string brand, out string model)
{
    brand = Brand;
    model = Model;
}

Default Values in Records

In Positional Records:

public record Person(string FirstName, string LastName = "Unknown");
var p = new Person("John"); // LastName = "Unknown"

In Regular Records:

public record Car
{
    public string Brand { get; init; } = "Unknown";
}
var c = new Car(); // Brand = "Unknown"

Are Records a New Type?

No, records are still reference types. You can use GetType(), reflection, type checks, etc., just like with classes.

Can Classes Use Deconstruct?

Yes, but you must define the Deconstruct method manually. It works similarly to records once added.

When to Use Records?

  • ✅ When you want immutability
  • ✅ When data comparison should be by value
  • ✅ For cleaner and safer DTOs and models
  • ❌ Avoid if your object needs to be mutable frequently

Need Help?

Contact Sudipto Kumar Mukherjee – Microsoft Certified Trainer with 20+ years of experience in .NET, C#, ASP.NET Core.

Phone: +91-93318-97923
Location: Dum Dum, Kolkata
Email: supernova.software.solutions@gmail.com

Any Query / Enrollment Request



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50