What is Record in C#?

In C# 9.0 and later versions, the record keyword is introduced as a new reference type to define immutable data structures more conveniently. Records provide a concise syntax for defining classes whose primary purpose is to store data and carry state. They are designed to simplify the creation of simple, immutable objects and come with built-in functionality for value equality, immutability, and pattern matching.

Here are some key features and characteristics of records in C#:

  1. Immutable Data:
    • Records are immutable by default, meaning their properties cannot be modified once assigned.
    • Properties within a record are automatically initialized upon object creation, and their values cannot be changed afterward.
    • Any modification to a property results in the creation of a new record with the updated property value.
  2. Value Equality:
    • Records provide structural equality by default. Two record instances are considered equal if their property values are structurally equal.
    • Records generate value-based equality implementations, including the Equals() method, GetHashCode() method, and the == and != operators.
    • The value equality behavior simplifies comparison and enables records to be used in collections like dictionaries and hash sets.
  3. Property Declaration:
    • Properties within a record are declared using the same syntax as regular classes, including access modifiers, property names, and data types.
    • Records also support computed properties and readonly properties.
  4. Built-in Methods and Members:
    • Records automatically generate a set of methods and members, including constructors, property accessors, equality members, and more.
    • Records provide a default constructor that initializes all properties based on their types.
    • Additional constructors can be defined to provide custom initialization logic.
  5. Deconstruction and Pattern Matching:
    • Records support deconstruction, allowing you to extract property values into individual variables using the deconstruct pattern.
    • Pattern matching can be used with records, enabling concise and powerful matching patterns in switch statements and other pattern matching scenarios.

Here’s an example that demonstrates the usage of records:

public record Person(string FirstName, string LastName)
    // Additional properties, methods, and customizations can be added here

// Usage of the Person record
Person person1 = new Person("John", "Doe");
Person person2 = new Person("John", "Doe");

bool areEqual = person1 == person2;  // Value equality comparison

In this example, the Person record is defined using the record keyword. It has two properties: FirstName and LastName. The record automatically generates value equality members, including the Equals() method, GetHashCode() method, and the == and != operators. The == operator is used to compare two Person records for value equality.

Records provide a concise and expressive way to define immutable data structures, focusing on data storage and value-based equality. They are particularly useful when working with data transfer objects (DTOs), message passing, or any scenario where immutable data storage is required.

error: Content is protected !!