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 syntaxpublic 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);Characteristics
Section titled “Characteristics”- Reference type (allocated on heap)
- Immutable by default (
initaccessors) IEquatable<T>implementation automatically provided- Overrides
Equals,GetHashCode, andToString
Real-world usage example
Section titled “Real-world usage example”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.