C# Programming
Published in

C# Programming

C# 9.0 record types

Believe me, I was very excited when I see this feature in C# 9.0. As a programmer I got many situation where I had to do a lot to compare two class objects to check if they both have same values, cloning a class and used to think why can’t be this made simpler.

And here we go, Now C# 9.0 has introduced record types to fulfil the need of value based equality, comparisons with reference variables. So the difference between a class and record type are:

  1. Both are reference type but record type equality is determined by the equality of values of the data members where as class does the comparison with reference.

Yeah and that is all the difference I see between a class and record type. ❤

I was playing a bit to figure out what we can do with record type and below code shows all interesting stuff or need which we want as a programmer. So let’s go one by one.

  1. Is record type supports interface inheritance: Yes It does.
    public interface IHuman
string Family { get; }

public record Person : IHuman
public string Family => "Hominidae";
public string FirstName { get; init; }
public string LastName { get; init; }

Note: record type too support init-only feature of C# 9.0 which is equivalent to ReadOnly. In above code I have mentioned init only setter property for FirstName and LastName which means these values must be initialized while object initialization and can’t be modified after that.

2. Is record type support inheritance: Yes It does.

    public record Actor : Person
public int Age { get; set; }
public Gender Gender { get; init; }
public void PrintActorDetails()
Console.WriteLine($"Name:{FirstName} {LastName}, Age:{Age}, Gender:{Gender}");

Also you can introduced new record types with inheritance simply with a line of code:

public record Bollywood(string Industry) : Actor;

This means, the new Bollywood record type has a new member called Industry which is required while creating a new object of record type Bollywood. Interesting Isn’t it. I ❤ it.

3. Can we clone the object of record type: Aha, it supports C# 9.0 ‘with’ expression which allows to easily clone the object.

var actor1 = new Actor { FirstName = "Varun", LastName="Dhawan", Age=30, Gender=Gender.Male };
var actor2 = actor1 with { FirstName = "Akshay", LastName= "Kumar" };

4. It really compares values: Yes it does.

var actor1 = new Actor { FirstName = "Varun", LastName="Dhawan", Age=30, Gender=Gender.Male };
var actor2 = actor1 with { Age = 30 };
Console.WriteLine(actor1.Equals(actor3));// results true.

5. Last but not the list, Everyone would love it for tracing (no more serializing).

var bollyActor = new Bollywood("Bollywood") { Age = 30, FirstName = "Kiara", LastName = "Advani", Gender = Gender.Female };Console.WriteLine(bollyActor);

Above code will result output as:

Bollywood { Family = Hominidae, FirstName = Kiara, LastName = Advani, Age = 30, Gender = Female, Industry = Bollywood }

Hope you enjoyed reading! Thank You.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Binod Mahto

Binod Mahto

Solution Architect & Full Stack Developer. Passionate about Software designing & development and Learning Technologies and Love to share what I learn.