.NET C# Record Type Features

Abhishek Malaviya
3 min readSep 24, 2023

--

As many of you may already be aware, C# 9 introduced a groundbreaking type known as “Record” . These Records seamlessly blend the versatility of classes with the inherent immutability of structs, while also offering a host of innovative features. Let’s delve into the intricacies of this exciting development features:

Sample Record

This record will use for all our illustrative examples.

public record Person(string FirstName, string LastName, 
short Age, string EmailID);

Automatic Properties

Records automatically generate read-only properties (the properties declared in the record). These properties can be accessed directly, but you cannot modify them.

class Program
{
static void Main()
{
Person person = new("Andrew", "Johnson", 30, "AndrewJohnson@xyz.com");

//properties can be accessed directly, but you cannot modify them.
Console.WriteLine($"First Name:{person.FirstName}");
Console.WriteLine($"Last Name:{person.LastName}");
Console.WriteLine($"Email ID:{person.EmailID}");
}
}

Output

Record in C#

Note: Records leverage a primary constructor behind the scenes, hence making it mandatory for all parameters to be required.

Immutability

Records are immutable by default, meaning that once you create an instance, you cannot modify its properties. This immutability ensures that once a record is created, its state remains constant, making it easier to reason about and preventing unintended side effects. So it is right candidate for DTO(Data Transfer Object).

Record is immutable.

How to modify a record

To modify a record, you create a new record instance with the desired changes. Since records are immutable, this process effectively creates a new object with the updated values:

personWithAgeModified = person with { Age = 40 };

In this example, we are creating a new Person record based on the person record with a modified Age. The with expression creates a new record instance, copying all properties from the original person except for the Age, which is set to "40" .

It’s important to note that the original person object remains unchanged. The modified record is a new instance with the updated values. This immutability ensures that the original state is preserved and makes it easier to reason about the behavior of your code.

class Program
{
static void Main()
{
Person person = new("Andrew", "Johnson", 30, "AndrewJohnson@xyz.com");
Person personWithAgeModified = person with { Age = 40 };

//Here we can see original record person is not modified
Console.WriteLine($"Age of {nameof(person)} object:{person.Age}");
Console.WriteLine($"Age of {nameof(personWithAgeModified)} object:{personWithAgeModified.Age}");
}
}
Record modify using with operator

Value Equality

Records provide value-based equality semantics by default. Two record instances with the same property values are considered equal. This is achieved through automatically generated Equals, GetHashCode, and ==/!= operators.

In the following example, both “person1” and “person2” share identical properties with matching values, making them indistinguishable from each other.

//Usage Example
Person person1 = new("Andrew", "Johnson", 30, "AndrewJohnson@xyz.com");
Person person2 = new("Andrew", "Johnson", 30, "AndrewJohnson@xyz.com");

Console.WriteLine($"{person1.Equals(person2)}"); //Output: True

Deconstruction

Records support deconstruction, allowing you to easily extract the values of their properties using the deconstruction syntax. If you’re immersed in front-end technology, you’re likely familiar with the fact that JavaScript already incorporates this feature.

Person person = new("Andrew", "Johnson1", 30, "AndrewJohnson@xyz.com");
var (firstName, lastName, age, emailId) = person;

Console.WriteLine($"Full Name: {firstName} {lastName}");

Output

In summary, C# records provide a concise and convenient way to define data structures with immutability, value-based equality, and other features that are often required when working with data. They are particularly useful for DTOs (Data Transfer Objects), data modeling, and other scenarios where simple data representation is required.

Thanks for reading.

--

--

Abhishek Malaviya

AI, Cloud, DevOps, Microservice, .NET, Sitecore, JavaScript, Database