Records in C#

C# 9.0 supports records: Classes that receive auto-generated functions that faciliate a value-like comparison behaviour. They are well-suited for immutability, i.e. to not change after creation.

The introductory page shows a couple of usage patterns:

Declaration via positional parameters:

public class Duck(int Age, string Name);

Construction just like regular classes:

var myPetDuck = new Duck(3, "Peter Pan");

Copy updates (“non-destructive mutation”):

var petersPetDuck = myPetDuck with { Name = "Linkerbell" };

This copy is a shallow-copy, i.e. reference values are copied and link to the same sub-object.

Still to check:

  • Pattern matching with records