Many of the C# classes created today are simply collections of data to be transferred from one place to another.
C# 9 introduces records, a new reference type that you can create instead of classes or structs. Records are distinct from classes in that record types use value-based equality. , see the official website.
Records have a few important behaviours:
- they are immutable;
- they are comparable to other records through value equality by default;
- they support non-destructive mutation through a new “with” keyword;
- they generates automatic deconstructors to match their primary constructors.
Let’s see a simple example.
First create a folder and create a new proiect:
1 2 3 4 5 6 7 8 9 10 11 12 | [mythcat@desk CSharpProjects]$ cd Record001/ [mythcat@desk Record001]$ dotnet --version 6.0.100-preview.5.21302.13 [mythcat@desk Record001]$[mythcat@desk Record001]$ dotnet new console The template "Console Application" was created successfully. Processing post-creation actions... Running 'dotnet restore' on /home/mythcat/CSharpProjects/Record001/Record001.csproj... Determining projects to restore... Restored /home/mythcat/CSharpProjects/Record001/Record001.csproj (in 134 ms). Restore succeeded. [mythcat@desk Record001]$ code Program.cs |
The source code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | using System; namespace Record001 { class Program { // define data for pattern matching enum Programming_language { C, Csharp, Python } /* // define a simple class Person public class Person { public string FirstName { get; private set; } public string Surname { get; private set; } public Person(string firstName, string surname) { FirstName = firstName; Surname = surname; } } */ // I can use a record like this: //public record Person(string FirstName, string Surname); abstract record Person; //can also support inheritance with class Person record Employee(params string[] JobResponsibilities) : Person; record Manager(int NumberOfEmployee = 2, bool Programmer = true): Person; record Programmer(Programming_language csharp = Programming_language.Csharp): Person; static void Main(string[] args) { //Console.WriteLine("Hello World!"); Employee employee_one = new Employee("will solve 1% of project!"); // this is pattern matching by Type check and declaration combination if (employee_one is Employee human) { Console.WriteLine($"this employee {string.Join(", ", human.JobResponsibilities)}"); } } } } |
You can see how the Person class is replaced by a record:
1 2 3 4 5 | //I can use a record like this for Person class and is replaced by a record //public record Person(string FirstName, string Surname); // or use an simple abstract record abstract record Person; |
You can inherit this class and use it as other classes
1 2 3 | record Employee(params string[] JobResponsibilities) : Person; record Manager(int NumberOfEmployee = 2, bool Programmer = true): Person; record Programmer(Programming_language csharp = Programming_language.Csharp): Person; |
You can also see a pattern matching mode involving an old keyword, if, and a new keyword, is.
After running the program I got this output:
1 2 | [mythcat@desk Record001]$ dotnet run Program.cs this employee will solve 1% of project! |