Skip to content
Tutorials Center
My learning

Topics

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.

C#
public record Person(string Name, int Age);
var older = new Person("Ana", 30) with { Age = 31 };

Try it

C# Equality, with and deconstruction

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.

C# Inheritance, custom members and record struct

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

C#
[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 a List<T> is equal only if the lists are the same instance, not if their contents match.
  • with is 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 Student never equals a Person.
  • Records are not automatically immutable. Properties you add with { get; set; } are mutable; only the positional ones are init-only.
  • sealed ToString: since C# 10 you can seal ToString in a record so derived records do not override it.
  • Use a record struct for small value data. Use a record class for entities, messages, DTOs and anything compared by value.
  • Attributes on positional parameters can be targeted with property: or field:.