C# / .NET Compendium PLEN
.NET / C# / C# — language fundamentals

The difference between a class and a record in C#

The fundamental difference - equality semantics

public class OrderClass
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
}

public record OrderRecord(int Id, string CustomerName);
var a = new OrderClass { Id = 1, CustomerName = "Jan" };
var b = new OrderClass { Id = 1, CustomerName = "Jan" };
Console.WriteLine(a == b);        // false - DIFFERENT objects in memory (reference comparison)
Console.WriteLine(a.Equals(b));   // false - to samo

var c = new OrderRecord(1, "Jan");
var d = new OrderRecord(1, "Jan");
Console.WriteLine(c == d);        // true - the SAME values (value comparison)
Console.WriteLine(c.Equals(d));   // true - to samo

Class - by default reference equality (is it the same object in memory). Record - by default value equality (do all properties have the same values).

Where this comes from - what the compiler generates

For a record the compiler automatically generates: a constructor, init properties, overloaded Equals()/GetHashCode() based on comparing all fields, ToString(), the ==/!= operators, and a Deconstruct() method. For a class you have to write all of this by hand if you want similar behaviour.

Mutability - another key difference

public class OrderClass
{
    public int Id { get; set; } // set - can be changed at any time
}

public record OrderRecord(int Id); // init - tylko przy tworzeniu

var orderClass = new OrderClass { Id = 1 };
orderClass.Id = 2; // ✅ OK - a class is mutable by default

var orderRecord = new OrderRecord(1);
orderRecord.Id = 2; // ❌ Compile error - init-only, can't be changed after creation

Changing an immutable record - with:

var updated = orderRecord with { Id = 2 }; // a NEW instance, the original is unchanged

ToString() - a readable representation "for free"

var orderClass = new OrderClass { Id = 1, CustomerName = "Jan" };
Console.WriteLine(orderClass);
// OrderClass - the default ToString() from object, useless (unless you override it by hand)

var orderRecord = new OrderRecord(1, "Jan");
Console.WriteLine(orderRecord);
// OrderRecord { Id = 1, CustomerName = Jan } - AUTOMATYCZNIE czytelny format

Deconstruction - built into record

public record OrderRecord(int Id, string CustomerName);

var order = new OrderRecord(1, "Jan");
var (id, customerName) = order; // works right away, without writing your own Deconstruct()

For a class you would have to implement Deconstruct() by hand.

Syntax - positional vs "classic" record

// Positional (shortened) - the most commonly used
public record OrderDto(int Id, string CustomerName, decimal TotalAmount);

// The "classic" record syntax - like a class, but with value equality
public record OrderDto
{
    public int Id { get; init; }
    public string CustomerName { get; init; }
    public decimal TotalAmount { get; init; }
}

The second form is sometimes needed when you want extra validation logic in the constructor or computed properties.

Inheritance - both support it, but record has additional equality rules

public record Animal(string Name);
public record Dog(string Name, string Breed) : Animal(Name);

var dog1 = new Dog("Rex", "Labrador");
var dog2 = new Dog("Rex", "Labrador");
dog1 == dog2; // true - ale UWAGA:

Animal animal = dog1;
Animal sameAnimal = new Dog("Rex", "Labrador");
animal == sameAnimal; // true - a record also compares the RUNTIME TYPE, not just the base properties!

This is a subtle difference compared to class - a record checks whether the compared objects are exactly the same type at runtime, so a Dog will never be "equal" to a hypothetical other type inheriting from Animal, even with the same field values.

record class vs record struct

public record OrderDto(int Id);           // by default: record CLASS (reference type, on the heap)
public record struct PointDto(int X, int Y); // record STRUCT (value type, on the stack)

record struct combines the value semantics of record with the memory semantics of struct - copying by value, no heap allocation. Useful for small, simple data.

Comparison table

class record
Default equality Reference (the same object) Value (the same data)
Mutability Mutable by default (set) Immutable by default (init)
ToString() Default, useless Automatic, readable
Deconstruction Has to be written by hand Built in (for positional)
with (copy with a change) Not supported Natively supported
Memory type Reference (heap) Reference (heap), unless record struct
When to use Entities with identity, objects with behaviour and mutable state Data (DTO, Command/Query), values where "what" matters, not "which one"

Which to choose - a practical rule

record: - DTOs in Web API (OrderDto) - Commands/Queries in CQRS (GetOrderQuery(int OrderId)) - Anywhere two objects with the same data should be considered "the same" - When you want immutability by default

class: - EF Core entities (an Order from the database) - they require mutation for change tracking (order.Status = "Shipped"), and a record with init-only properties would make that impossible - Services, ViewModels, repositories - objects with behaviour, not just data - When the identity of the object matters regardless of field values (two Orders with the same Id but a different Status over time are still conceptually "the same" order - that doesn't fit "equality by all values")

A trick question in an interview

"Should an EF Core entity be a record?" - Usually not. EF Core relies on change tracking through property mutation (as we discussed with Change Tracking) - it needs set, not init. Besides, the "equality by identity" semantics (the same database row, identified by the primary key) fits class better than the "equality by all values" of record - two Orders with the same Id but one changed property are conceptually still "the same" order, while a record would consider them different objects (because they differ in value).

Buy me a coffee