Skip to content
Tutorials Center
My learning

Topics

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.

C#
public record Person(string Name, int Age);
public readonly record struct Point(int X, int Y);

Try it

C# Value equality, with and ToString

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 and record structs

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#
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 a List<T> inside a record compares by reference. Copying with with also copies references, not contents.
  • with runs the object initializer on a copy, so it works with init setters. It does not run constructor validation; put validation in init accessors 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 HashSet or as a dictionary key.
  • Inheritance in record classes compares the runtime type too, so Animal("Rex") is not equal to Dog("Rex", ...) even when shared members match. Mark records sealed when 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.
  • record vs record struct: the class form allocates and supports inheritance; the struct form avoids allocation for small values. Prefer readonly record struct for value objects.
  • Override ToString to customize output; since C# 10 you may mark it sealed to stop derived records from regenerating it.
  • Pattern matching works naturally with the generated deconstructor and properties: p is { Age: > 30 } or p is Person(var n, _).