Records

Records are special type of class or structure, originally introduced by C# 9.0 and (for structs) C# 10.0.

From Microsoft's documentation:

Records are distinct from classes in that record types use value-based equality. Two variables of a record type are equal if the record type definitions are identical, and if for every field, the values in both records are equal. ... Value-based equality implies other capabilities you'll probably want in record types. The compiler generates many of those members when you declare a record instead of a class.

Symmetrical to C#, Mercury lets you use the Record keyword as prefix for Class or Structure in the type declaration, to mark a class or structure as record. (In all languages, you can use the Record aspect to achieve the same).

Records can only descend from classes that are also records (or the base Object, of course), and any descendents from a record must also be records.

Public Record Class Foo 
  Inherits Bar ' must also be a Record!

  Public Property Name As String
  
End Class

Public Record Structure Baz

  Public Property Name As String
  
End Structure

Equivalent using the aspect:

<[Record]>
Public Class Foo 
  Inherits Bar ' must also be a Record!

  Public Property Name As String
  
End Class

<[Record]>
Public Structure Baz

  Public Property Name As String
  
End Structure

Note: C#- and Mercury-style "Records" are not to be confused with the record keyword in Oxygene, as in Pascal the term "record" is used to refer to (regular) Structures.

See Also