Introducing C# 9.0's Record Datatype: Safe, Efficient, and Easy to Use
Tags: csharp, dotnet, programming, abotwrotethis
Published: 2023-04-14 10:55:27

C# 9.0 introduces a new datatype called `record`, which is an immutable type that provides value-based equality. Records are ideal for representing data that is primarily for storage and retrieval. In this blog post, we'll explore the features and benefits of records, how to create them, and how they can be used in C# code through code examples.

**Features and Benefits**

The `record` datatype in C# has several features and benefits, including:

_Immutability_: Records are immutable, which means their values cannot be changed once they're created. This feature ensures records are safer to use because they can't be modified after they're created.

_Value-Based Equality_: Records provide value-based equality, which means that two records are equal if their values are equal. This feature makes it easy to compare records and determine if they're the same.

_Built-In Deconstruction_: Records have built-in deconstruction, which allows you to easily extract their values and assign them to variables. This feature makes it easy to work with records and use their values in your code.

_Concise Syntax_: The syntax for creating records is concise, making it easy to create and use them in your code. This feature saves time when writing code and makes it easier to work with records.

**Creating Records**

To create a record in C#, you use the record keyword followed by the name of the record and its properties. Here's an example:

```C#
public record Person(string FirstName, string LastName, int Age);
```

In this example, the `Person` record has three properties: `FirstName`, `LastName`, and `Age`. These properties are used to store the person's first name, last name, and age.

Using Records

Records can be used in many ways in C# code. Here are some examples:

Creating a list of records:

```C#
List<Person> people = new List<Person>()
{
    new Person("John", "Doe", 30),
    new Person("Jane", "Doe", 25)
};
```

In this example, we've created a list of Person records and added two records to the list. We've also specified the values for each property when we created the records.

Using records as function return types:

```C#
public record Person(string FirstName, string LastName, int Age);

public Person GetPerson()
{
    return new Person("John", "Doe", 30);
}

var person = GetPerson();
```


In this example, we've created a function that returns a Person record. We've then assigned the returned record to a variable, which can be used later in the code.

Deconstructing records:

```
var person = new Person("John", "Doe", 30);
var (firstName, lastName, age) = person;
```


In this example, we've created a Person record and then deconstructed it to extract its values. The values are then assigned to variables, which can be used later in the code.

**Conclusion**

In conclusion, the new record datatype in C# 9.0 is a powerful tool that can be used to store and retrieve data in a safe and efficient way. Its features, such as immutability, value-based equality, built-in deconstruction, and concise syntax, make it easy to use and save time when writing code. Records can help streamline code and improve its readability and maintainability. Whether you're working on a small project or a large-scale application, records can be a useful addition to your C# toolbox.