DEV Community

Wriju's Blog
Wriju's Blog

Posted on • Updated on

C# 9.0 Inheritance in Record Type

A record in C# 9.0 can inherit from another record. This is one of the strong reasons why you should consider using record over struct.

var student = new Student() { FullName = "Wrishika Ghosh", Grade = "V" };

public record Person
{
    public string FullName { get; set; }
}

public record Student : Person
{
    public string Grade { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

You can even simplify it by using the positional approach via auto construct,

var student = new Student("Wrishika Ghosh", "V");

public record Person(string FullName);

public record Student(string FullName, string Grade) : Person(FullName);
Enter fullscreen mode Exit fullscreen mode

Discussion (0)