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

Class Guidelines for Effective 1-on-1 Learning

To keep every session productive and distraction-free, please follow these simple guidelines:

  • Quiet Environment: Join from a calm, private room with minimal background noise. Avoid public or noisy places.
  • No Interruptions: Inform family/roommates in advance. Keep doors closed during class.
  • Mobile on Silent / DND: Set your phone to Silent or Do Not Disturb to prevent calls and notifications.
  • Be Fully Present: Do not multitask. Avoid attending to other calls, visitors, or errands during the session.
  • Stable Setup: Use a laptop/desktop with a stable internet connection and required software installed (Visual Studio/.NET, SQL Server, etc.).
  • Punctuality: Join on time so we can utilize the full session effectively.
  • Prepared Materials (If any): Keep project files, notes, and questions ready for quicker progress.

Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.



Google Review Testimonials

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