C# 9.0 introduces a new type called record type which is an immutable reference type with a ‘built-in’ value-based comparison. It’s inumattble because once the object is created it cannot be changed.
The example below creates 2 structures. A Record and a regular Class
1
2
3
4
5
6
7
8
9
10
11
|
public record CarRecord
{
public string Maker { get; }
public CarRecord(string maker) => (Maker) = (maker);
}
public class CarClass
{
public string Maker { get;}
public CarClass(string maker) => Maker = maker;
}
|
Let’s create 2 objects with the same values and compare them.
1
2
3
4
5
6
7
8
9
|
//Record
var firstCar = new CarRecord("honda");
var secondCar = new CarRecord("honda");
Console.WriteLine(firstCar == secondCar); //true because a Record structure provides a value-based comparison
//Class
var firstCarClass = new CarClass("honda");
var secondCarClass = new CarClass("honda");
Console.WriteLine(firstCarClass == secondCarClass); //false because we are comparing 2 different instances
|
Records support inheritance and abstraction:
1
2
3
4
5
|
public record HondaRecord : CarRecord
{
public string Model { get; }
public HondaRecord(string model) : base("honda") => Model = model;
}
|
1
2
3
4
|
var firstCar = new CarRecord("honda");
var secondCar = new HondaRecord("crv");
Console.WriteLine(firstCar == secondCar); //false
Console.WriteLine(secondCar is CarRecord); //true
|
Records can also be declared in a more concise form:
1
2
|
public record CarRecord(string Maker);
public record HondaRecord(string Model):CarRecord("honda");
|
Creating a copy of a record:
1
2
3
4
|
var anotherSecondCar = secondCar; //creates a copy
var anotherCrv = secondCar with { }; //it also creates a copy
var civic = secondCar with { Model = "civic" }; //creates a copy with a new Model property value
Console.WriteLine(anotherCrv == civic); //false
|