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

Comparing record and class declarations in C# - syntax side by side

1. The simplest case - data without logic

// CLASS - the full, verbose syntax
public class OrderClass
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public decimal TotalAmount { get; set; }
}

// RECORD - positional syntax, jedna linijka
public record OrderRecord(int Id, string CustomerName, decimal TotalAmount);

This single record line automatically generates what in a class you have to write by hand (and more) - let's look at exactly what is hidden under this shortened syntax.

2. What the compiler actually generates for a record - the "expansion" of a positional record

To understand the difference, it is worth seeing what it roughly compiles to for public record OrderRecord(int Id, string CustomerName, decimal TotalAmount);:

// What you MUST write by hand in a class to get behaviour EQUIVALENT to a record:
public class OrderRecordEquivalent : IEquatable<OrderRecordEquivalent>
{
    // Properties - init, not set (immutability)
    public int Id { get; init; }
    public string CustomerName { get; init; }
    public decimal TotalAmount { get; init; }

    // Konstruktor pozycyjny
    public OrderRecordEquivalent(int id, string customerName, decimal totalAmount)
    {
        Id = id;
        CustomerName = customerName;
        TotalAmount = totalAmount;
    }

    // Equals (value-based, not reference-based)
    public override bool Equals(object obj) => Equals(obj as OrderRecordEquivalent);
    public bool Equals(OrderRecordEquivalent other)
    {
        if (other is null) return false;
        if (ReferenceEquals(this, other)) return true;
        return Id == other.Id && CustomerName == other.CustomerName && TotalAmount == other.TotalAmount;
    }

    // GetHashCode consistent with Equals
    public override int GetHashCode() => HashCode.Combine(Id, CustomerName, TotalAmount);

    // Operatory == i !=
    public static bool operator ==(OrderRecordEquivalent left, OrderRecordEquivalent right)
        => left is null ? right is null : left.Equals(right);
    public static bool operator !=(OrderRecordEquivalent left, OrderRecordEquivalent right) => !(left == right);

    // ToString - czytelna reprezentacja
    public override string ToString()
        => $"OrderRecordEquivalent {{ Id = {Id}, CustomerName = {CustomerName}, TotalAmount = {TotalAmount} }}";

    // Deconstruct - dekonstrukcja
    public void Deconstruct(out int id, out string customerName, out decimal totalAmount)
    {
        id = Id;
        customerName = CustomerName;
        totalAmount = TotalAmount;
    }

    // "with" requires a special copy constructor - in a real record generated automatically
    protected OrderRecordEquivalent(OrderRecordEquivalent original)
    {
        Id = original.Id;
        CustomerName = original.CustomerName;
        TotalAmount = original.TotalAmount;
    }
}

All of this is hidden under a single record line. This is probably the most convincing way to show the difference in an interview - record is not "a different keyword for the same thing", but a real reduction of boilerplate by several dozen lines.

3. Property declaration - set vs init

// CLASS - mutable by default (set)
public class OrderClass
{
    public int Id { get; set; }
}

var c = new OrderClass { Id = 1 };
c.Id = 2; // ✅ OK w dowolnym momencie

// RECORD (positional) - init-only by default
public record OrderRecord(int Id);

var r = new OrderRecord(1);
r.Id = 2; // ❌ Compile error

// A RECORD can also be made mutable if you explicitly use "set"
public record MutableOrderRecord
{
    public int Id { get; set; } // explicit "set" instead of the default init from positional syntax
}

This shows that a record is not immutable by definition - it is the positional syntax that generates init by default, but you can override that by writing the properties by hand with set.

4. The "classic" record syntax (non-positional) - closer in appearance to class

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

    public OrderClass(int id, string customerName)
    {
        Id = id;
        CustomerName = customerName;
    }
}

// RECORD - the "classic" syntax, very similar to a class, DIFFERS only in the keyword
public record OrderRecord
{
    public int Id { get; init; }           // init instead of set - but you can use "set" if you want mutability
    public string CustomerName { get; init; }

    public OrderRecord(int id, string customerName)
    {
        Id = id;
        CustomerName = customerName;
    }
}

The key takeaway: the word record instead of class on its own gives you the generated Equals/GetHashCode/ToString/==/!= operators - regardless of whether you use the shortened (positional) or the full syntax. Positional is an extra "bonus" (an automatic constructor + properties), but simply swapping class → record already changes the equality semantics.

5. Inheritance - almost identical syntax, different semantics

// CLASS
public class Animal
{
    public string Name { get; set; }
    public Animal(string name) => Name = name;
}
public class Dog : Animal
{
    public string Breed { get; set; }
    public Dog(string name, string breed) : base(name) => Breed = breed;
}

// RECORD - positional, much shorter
public record Animal(string Name);
public record Dog(string Name, string Breed) : Animal(Name); // "base" is called in the inheritance header

6. record class vs record struct - an extra axis that class doesn't have

public record OrderDto(int Id);              // by default: record CLASS (reference, heap)
public record class OrderDtoExplicit(int Id); // to samo, jawnie
public record struct PointDto(int X, int Y);  // record STRUCT (value, stack)

class has no equivalent of record struct - a plain struct does not automatically generate Equals/GetHashCode by value as precisely as record struct does (although a struct on its own compares fields by default via reflection in ValueType.Equals, which is slower than the generated code in record struct).

7. The sealed modifier with record inheritance - a subtle difference

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

The compiler automatically generates in Dog a method comparing EqualityContract (a protected property we saw indirectly with record inheritance) - this is a mechanism unique to record that class doesn't have at all, because class doesn't generate any automatic equality logic.

Summary - a table of differences in the declaration itself

Syntax element class record (positional) record (classic syntax)
Property declaration Has to be written explicitly Generated from the parameters Has to be written explicitly
Default accessor { get; set; } (if you write it that way) { get; init; } automatically You choose set/init yourself
Constructor Has to be written explicitly (or the default parameterless one) Generated from the positional parameters Has to be written explicitly
Equals/GetHashCode Inherited from object (reference) Generated (value) Generated (value)
ToString() Inherited from object (useless) Generated (readable) Generated (readable)
Deconstruct() Has to be written by hand Generated automatically Has to be written by hand
with expression Not supported Supported Supported
==/!= operators Inherited (reference), can be overloaded by hand Generated (value) Generated (value)

A minimal, concise answer to this question in an interview

"Syntactically, a positional record (record OrderDto(int Id, string Name);) is one line that replaces what in a class would require a separate property declaration, a constructor, and a manual override of Equals, GetHashCode, ToString and the ==/!= operators if we want value equality. Even the 'classic' record syntax (non-positional, with explicit properties) looks almost identical to a class, but the record keyword instead of class alone turns on the automatic generation of all this equality logic and ToString - it is a semantic difference hidden under a very similar syntax."

Buy me a coffee