1

I want to benefit both from the built-in cloning functionality of the record construct in c#, and from its extensibility. Based on this article, I have:

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

public record Student : Person
{
    public string Grade { get; set; }
}

These work:

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

I can also make a new Person from an existing Person using with a.k.a non-destructive mutation. But can I make a new Student from an existing Person in a similar manner - without explicitly specifying all properties that could be cloned as they are inherited? I am looking for a semantics like this: var student = new Student from person with { Grade = "V" }

ZorgoZ
  • 2,974
  • 1
  • 12
  • 34

0 Answers0