DEV Community

JKC
JKC

Posted on • Updated on

Introducing C# 9.0's Record Datatype: Safe, Efficient, and Easy to Use

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:

public record Person(string FirstName, string LastName, int Age);
Enter fullscreen mode Exit fullscreen mode

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:

List<Person> people = new List<Person>()
{
    new Person("John", "Doe", 30),
    new Person("Jane", "Doe", 25)
};
Enter fullscreen mode Exit fullscreen mode

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:

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

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

var person = GetPerson();
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
sloan profile image
Info Comment hidden by post author - thread only accessible via permalink
Sloan the DEV Moderator

Hey, this article seems like it may have been generated with the assistance of ChatGPT.

We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Could you review the guidelines and edit your post to add a disclaimer?

Some comments have been hidden by the post's author - find out more