public class Person { public Person() { } [SetsRequiredMembers] public Person(string firstName, string lastName) => (FirstName, LastName) = (firstName, lastName); public required string FirstName { get; init; } public required string LastName { get; init; } public int? Age { get; set; } } public class Student : Person { public Student() : base() { } [SetsRequiredMembers] public Student(string firstName, string lastName) : base(firstName, lastName) { } public double GPA { get; set; } }
The SetsRequiredMembers
disables the compiler’s checks that all required
members are initialized when an object is created. Use it with caution.
References
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
https://youtu.be/9CDgPgWF9IY