Introduction
In this article, I am going to explain Record types that have been introduced in C# 9.0. I have split this concept into multiple articles. This is the first article and can be used by a beginner, intermediate, and professional.
Record type or record is a very interesting feature introduced in C# 9.0. As we know, in F#, everything is considered immutable, and similarly, in C#, record types help us to work with immutable types.
Prerequisites
- .NET 5.0
- Visual Studio 2019 (V 16.8, Preview 3)
What is a record type in C#
Before we start on the record type, you should have a basic understanding of Init properties.
I suggest you go through this article first if you are not aware of Init-Only Property.
Do you remember that Microsoft announced data class in a previous release? That data class is called record type in the new release. As the name suggests, it is a record, not data.
Let's understand the below concept first in simple words.
- Immutable means it cannot change. By default, Record types are immutable.
- Record Type is a compact and easy way to write reference types (immutable) that automatically behave like value types.
Let’s discuss the below code snippet.
public class Member
{
public int ID { get; init; }
public string FirstName { get; init; }
public string LastName { get; init; }
public string Address { get; init; }
}
Init properties are immutable properties which means member class properties cannot change. What will you do if you want to add a new property in the Member class (like the middle name) or change the value of the address property? The only way is you have to create new objects and then assign a new value, right?
Here I am going to create a new Membar class object and assign values to it.
var member = new Member
{
Id=1,
FirstName="Kirtesh",
LastName="Shah",
Address="Vadodara"
};
Assume that you want to change the address from “Vadodara ” to “Mumbai”.
See the below code.
var new member = new Member
{
Id = member.Id,
FirstName = member.FirstName,
LastName = member.LastName,
Address = "Mumbai"
};
Now suppose we want to add a new property, Middle Name, in the Member class.
public class Member
{
public int ID { get; init; }
public string FirstName { get; init; }
public string MiddleName { get; init; }
public string LastName { get; init; }
public string Address { get; init; }
}
var member = new Member
{
Id=1,
FirstName="Kirtesh",
MiddleName = ”D”,
LastName="Shah",
Address = "Vadodara"
};
var newMember = new Member
{
Id = member.Id,
FirstName = member.FirstName,
MiddleName = member.MiddleName,
LastName = member.LastName,
Address = "Mumbai"
};
What will you do if you have hundreds of properties? It will become so difficult, right? Hence C# 9.0 introduced Record type to work with immutable data and solve the above issue.
How to create Record Type?
Let’s try to implement Record Type in the Member class. See the below code.
public record Member
{
public int ID { get; init; }
public string FirstName { get; init; }
public string LastName { get; init; }
public string Address { get; init; }
}
What’s the difference between the earlier Member class and the new Member record type?
The only change is we have replaced the “Class” keyword with “Record”. Now Member record type is treated as an immutable data value.
With expression
C# 9.0 introduces the “With” expression with RecordType. It is mainly used to create new objects more effectively.
Before we understand the “With” expression, let's discuss the below code.
public record Member
{
public int ID { get; init; }
public string FirstName { get; init; }
public string LastName { get; init; }
public string Address { get; init; }
}
var member = new Member
{
Id=1,
FirstName="Kirtesh",
LastName="Shah",
Address = "Vadodara"
};
var newMember = new Member
{
Id = member.Id,
FirstName = member.FirstName,
LastName = member.LastName,
Address = "Mumbai"
};
Now we will use the “With” expression to write the above code. See the below code snippet.
var member = new Member
{
Id=1,
FirstName="Kirtesh",
LastName="Shah",
Address = "Vadodara"
};
var newMember = member with { Address = "Mumbai" };
The “With” expression is used as a {} syntax that allows you to define new values for a specific property. It will make development faster, easier, and cleaner.
That’s all for this article. To continue learning about record types, read C# Record Types Part 2
I hope you enjoy this article and find it useful.