Skip to main content

How to Declare a Positional Record in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

record Book(string Title, string Author, int Year); is a whole class in one line: three properties, a constructor, value-based equality and a ToString that shows the values. Its properties can't change once it's created.

Microsoft's reference lists what the compiler creates from the parameters: A public automatically implemented property for each positional parameter provided in the record declaration, which for a record is An init-only property, and A primary constructor whose parameters match the positional parameters on the record declaration.

Syntax​

Syntax
record <Name>(<Type1> <Property1>, <Type2> <Property2>);

<Name> <value> = new(<value1>, <value2>);
<Name> <copy> = <value> with { <Property1> = <newValue> };
PartWhat it is
record <Name>(...)Declares a class with one property per parameter
new(<value1>, <value2>)Creates a record, with the properties in their declared order
<value> with { ... }A copy with the properties you list changed
==Compares the properties' values

The reference on equality: For types with the record modifier (record class, record struct, and readonly record struct), two objects are equal if they are of the same type and store the same values.

The Example​

A console app called PositionalRecord creates two books with the same values and a copy with a different year, prints them, and compares the first two.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n PositionalRecord
cd PositionalRecord
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/PositionalRecord/PositionalRecord.csproj:
Determining projects to restore...
Restored /Users/you/source/PositionalRecord/PositionalRecord.csproj (in 46 ms).
Restore succeeded.

dotnet new console -n PositionalRecord creates a folder named PositionalRecord with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or PositionalRecord.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the books:

Program.cs (the books)
Book first = new("Learning C#", "Asha Rao", 2024);
Book same = new("Learning C#", "Asha Rao", 2024);
Book updated = first with { Year = 2025 };

first with { Year = 2025 } copies first and changes only the year.

Then print and compare them:

Program.cs (the output)
Console.WriteLine(first);
Console.WriteLine(first == same);
Console.WriteLine(updated);

And declare the record at the end of the file:

Program.cs (the record)
// One line: three properties, a constructor, equality and ToString.
record Book(string Title, string Author, int Year);

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Book { Title = Learning C#, Author = Asha Rao, Year = 2024 }
True
Book { Title = Learning C#, Author = Asha Rao, Year = 2025 }

macOS-style terminal window titled Terminal — PositionalRecord showing dotnet run printing Book with Title Learning C#, Author Asha Rao and Year 2024, then True, then the same book with Year 2025

  • The first line is the ToString the compiler wrote: the type and every property with its value.
  • True: first and same are two objects, but they hold the same values, so they're equal.
  • The last line is the copy, with the new year. first itself still says 2024.

The Complete File​

macOS-style code window titled Program.cs — PositionalRecord showing two books created with the same title, author and year, a copy made with the with expression and a new year, three Console.WriteLine calls, and the one-line record Book declaration

Program.cs
Book first = new("Learning C#", "Asha Rao", 2024);
Book same = new("Learning C#", "Asha Rao", 2024);
Book updated = first with { Year = 2025 };

Console.WriteLine(first);
Console.WriteLine(first == same);
Console.WriteLine(updated);

// One line: three properties, a constructor, equality and ToString.
record Book(string Title, string Author, int Year);

Common Mistakes​

MistakeWhat happensDo this instead
Changing a property after creating the recordfirst.Year = 2025; doesn't build: error CS8852: Init-only property or indexer 'Book.Year' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.Make a changed copy with with
Expecting a plain class to compare like a recordTwo objects of a class with the same title compared as False: for a class, == asks whether it's the same objectUse a record when equal values should mean equal objects

FAQ​

How do I get the values back out?​

A positional record can be taken apart: var (title, author, year) = first; printed Learning C# by Asha Rao, 2024.

Record or readonly record struct?​

A record is a class, a reference type. The reference: A record or a record class declares a reference type... A record struct declares a value type.

Conclusion​

record Book(string Title, string Author, int Year); gives you a class with init-only properties, a constructor, value equality, a readable ToString and changed copies through with. Use it for data that shouldn't change once it's created.

Additional Resources​

  • Records — positional syntax, equality and with.