Skip to content

record class

Context: record class (or simply record) is a reference type that behaves like a class but with value semantics for equality.

// Standard record class syntax
public record class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
// Positional syntax (compiler generates init properties and deconstructor)
public record class Employee(string Name, int Id);
  • Reference type (allocated on heap)
  • Immutable by default (init accessors)
  • IEquatable<T> implementation automatically provided
  • Overrides Equals, GetHashCode, and ToString

Domain entities in event sourcing: Records are used to represent events (e.g., UserCreated, OrderShipped) because they are immutable and equality is based on content, not identity.

Example: In C#, System.Environment is not a record, but records are heavily used in functional programming libraries like LanguageExt.