C# Reference ยท Keywords and Statements
C# records
Reference for C# records: positional syntax, value equality, non-destructive mutation with with, deconstruction, inheritance and record struct.
record declares a type whose main job is to carry data. The compiler generates value-based equality, a readable ToString(), a copy constructor, a with expression for copying with changes, and, for positional records, a Deconstruct method. Two records are equal when their types match and all their fields are equal, not when they are the same object.
record alone, or record class, creates a reference type. record struct (C# 10) creates a value type. A positional record declares its properties in the header; these are init-only (immutable after construction) for record class and readonly record struct, and mutable for a plain record struct.
public record Person(string Name, int Age); var older = new Person("Ana", 30) with { Age = 31 };
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
[modifiers] record [class] Name(Type Param, ...) [: Base(args)] [;|{ members }] [modifiers] record struct Name(Type Param, ...) [{ members }] [modifiers] readonly record struct Name(Type Param, ...) expression with { Property = value, ... }
| Generated member | Behavior |
|---|---|
| Properties | One per positional parameter: { get; init; } (class, readonly struct) or { get; set; } (plain struct). |
Equals, ==, !=, GetHashCode |
Compare the runtime type (classes) and every field. |
ToString() |
Name { Prop = value, ... }. |
Deconstruct |
Only for positional records, in parameter order. |
<Clone>$ and copy constructor |
Used by with. |
PrintMembers |
Helper used by ToString; can be customized. |
Errors: assigning an init property outside construction or an object initializer is compile error CS8852.
Usage notes
- Shallow equality. Fields are compared with their own
Equals. A record holding aList<T>is equal only if the lists are the same instance, not if their contents match. withis a shallow copy. The copy shares reference-typed fields with the original.- Inheritance: a record class can inherit from another record class (not from an ordinary class, and not the reverse). Equality includes the exact runtime type, so a
Studentnever equals aPerson. - Records are not automatically immutable. Properties you add with
{ get; set; }are mutable; only the positional ones are init-only. sealedToString: since C# 10 you can sealToStringin a record so derived records do not override it.- Use a
record structfor small value data. Use arecord classfor entities, messages, DTOs and anything compared by value. - Attributes on positional parameters can be targeted with
property:orfield:.
Related
kw-class,kw-struct,kw-initop-patternfor property and positional patterns- Tutorials: Records, Record patterns in practice