What's the use of the record type in C# (in ASP.NET Core MVC)?

Viewed 68

Recently I discovered a third type of object in C#: the record; actually 2: the record struct and the record class. As I understand the type does not differ significantly from normal classes (I never used structs in my programs). The big advantage seems to me that they are easier to construct, because of their constructor+property-in-1 syntax. Is this a correct point of view? I would like to use them then for building my View Models in the ASP.NET Core MVC framework. I don't know why they just added the syntax to classes? The doubt that I miss something holds me back from using them.

1 Answers

Below are some advantages scenarios where you can leverage the use of record type, check the performance part :)

- Concise syntax for creating a reference type with immutable properties

You can use positional parameters to declare properties of a record and to initialize the property values when you create an instance

public record Person(string FirstName, string LastName);

public static void Main()
{
    Person person = new("Nancy", "Davolio");
    Console.WriteLine(person);
    // output: Person { FirstName = Nancy, LastName = Davolio }
}
  • Built-in behavior useful for a data-centric reference type:

    - Value equality

    For class types, two objects are equal if they refer to the same object in memory while For record types, including record struct and readonly record struct, two objects are equal if they are of the same type and store the same values.

    - Concise syntax for nondestructive mutation

    If you need to copy an instance with some modifications, you can use a with expression to achieve nondestructive mutation. A with expression makes a new record instance that is a copy of an existing record instance, with specified properties and fields modified.

       public record Person(string FirstName, string LastName)
       {
        public string[] PhoneNumbers { get; init; }
       }
    
       public static void Main()
      {
       Person person1 = new("Nancy", "Davolio") { PhoneNumbers = new 
       string[1] };
    
       Person person2 = person1 with { FirstName = "John" };
       Console.WriteLine(person2);
       // output: Person { FirstName = John, LastName = Davolio, 
       PhoneNumbers = System.String[] }
       Console.WriteLine(person1 == person2); // output: False
       .
       .
       .
       person2 = person1 with { };
       Console.WriteLine(person1 == person2); // output: True
    

    - Built-in formatting for display

    Record types have a compiler-generated ToString method that displays the names and values of public properties and fields. The ToString method returns a string of the following format:

    "<record type name> { <property name> = <value>, <property name> = 
       <value>, ...}"
    
  • Support for inheritance hierarchies

    A record can inherit from another record. However, a record can't inherit from a class, and a class can't inherit from a record.

  • Positional parameters in derived record types

    The derived record declares positional parameters for all the parameters in the base record primary constructor. The base record declares and initializes those properties. The derived record doesn't hide them, but only creates and initializes properties for parameters that aren't declared in its base record.

  • Equality in inheritance hierarchies

    For two record variables to be equal, the run-time type must be equal.

  • with expressions in derived records

    The result of a with expression has the same run-time type as the expression's operand. All properties of the run-time type get copied, but you can only set properties of the compile-time type

  • PrintMembers formatting in derived records

    The synthesized PrintMembers method of a derived record type calls the base implementation. The result is that all public properties and fields of both derived and base types are included in the ToString output

  • Deconstructor behavior in derived records

    The Deconstruct method of a derived record returns the values of all positional properties of the compile-time type. If the variable type is a base record, only the base record properties are deconstructed unless the object is cast to the derived type.

- Converting object is much performant!

Check this article

MSSource

Related