C# Reference ยท Types and Literals
Records in depth
Reference for C# records: record class and record struct, positional syntax, value equality, with expressions, inheritance and what the compiler generates.
A record is a type designed to carry data. The compiler writes the repetitive members for you: value-based equality, GetHashCode, a readable ToString, a copy constructor, and support for non-destructive mutation with with. There are two kinds: record (also written record class) is a reference type, and record struct is a value type.
The shortest form is positional: parameters after the name become properties and a constructor and a deconstructor are generated.
public record Person(string Name, int Age); public readonly record struct Point(int X, int Y);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public record Name(Type Param1, Type Param2) [: BaseRecord(Param1)] [, IInterface] { // optional body: extra members, validation, overrides } public record struct Name(Type Param); // mutable properties public readonly record struct Name(Type Param); // init-only
| What the compiler generates | Details |
|---|---|
| Properties | For positional parameters: init-only in a record and readonly record struct, read-write in a plain record struct. |
Equals, ==, !=, GetHashCode |
Compare all instance fields. A record class also compares the runtime type through an EqualityContract. |
ToString |
Prints Name { Prop = value, ... } (record structs use the same format). |
Deconstruct |
For positional records, in parameter order. |
Copy constructor and <Clone>$ |
Used by with; a record class has a protected copy constructor. |
Errors. with works on records, structs and anonymous types, not on ordinary classes. A record class can inherit only from another record class (or object), and a record struct cannot inherit at all.
Usage notes
- Equality is shallow. Reference-typed members are compared with their own
Equals, so aList<T>inside a record compares by reference. Copying withwithalso copies references, not contents. withruns the object initializer on a copy, so it works withinitsetters. It does not run constructor validation; put validation ininitaccessors if every copy must be checked.- Hash codes are based on the field values. Do not mutate a mutable record while it sits in a
HashSetor as a dictionary key. - Inheritance in record classes compares the runtime type too, so
Animal("Rex")is not equal toDog("Rex", ...)even when shared members match. Mark recordssealedwhen you do not plan to derive. - Positional parameters become properties, so you can add attributes with
[property: JsonPropertyName("x")]. Parameter names with a lowercase first letter still produce properties with the name you wrote. recordvsrecord struct: the class form allocates and supports inheritance; the struct form avoids allocation for small values. Preferreadonly record structfor value objects.- Override
ToStringto customize output; since C# 10 you may mark itsealedto stop derived records from regenerating it. - Pattern matching works naturally with the generated deconstructor and properties:
p is { Age: > 30 }orp is Person(var n, _).