Challenge - 5 Problems
Record Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of record inheritance with positional parameters
What is the output of this C# code using record inheritance with positional parameters?
C Sharp (C#)
public record Person(string Name, int Age); public record Employee(string Name, int Age, string Position) : Person(Name, Age); var emp = new Employee("Alice", 30, "Developer"); Console.WriteLine(emp);
Attempts:
2 left
💡 Hint
Remember that the derived record includes all properties from base and derived types in its ToString output.
✗ Incorrect
In C#, a record's ToString method automatically includes all properties declared in the record and its base records. Here, Employee inherits from Person and adds Position, so the output shows all three properties.
❓ Predict Output
intermediate2:00remaining
Value equality in record inheritance
What is the output of this C# code comparing two records with inheritance?
C Sharp (C#)
public record Person(string Name, int Age); public record Employee(string Name, int Age, string Position) : Person(Name, Age); var emp1 = new Employee("Bob", 25, "Tester"); var emp2 = new Employee("Bob", 25, "Tester"); Console.WriteLine(emp1 == emp2);
Attempts:
2 left
💡 Hint
Records compare by value, including all properties from base and derived records.
✗ Incorrect
Records in C# implement value equality by default, comparing all properties including those inherited. Since emp1 and emp2 have identical values, the equality operator returns True.
❓ Predict Output
advanced2:00remaining
Output of record inheritance with overridden ToString
What is the output of this C# code where a derived record overrides ToString?
C Sharp (C#)
public record Person(string Name, int Age) { public override string ToString() => $"Person: {Name}, {Age}"; } public record Employee(string Name, int Age, string Position) : Person(Name, Age); var emp = new Employee("Carol", 28, "Manager"); Console.WriteLine(emp);
Attempts:
2 left
💡 Hint
The derived record inherits the overridden ToString from the base record unless it overrides it again.
✗ Incorrect
Since Employee does not override ToString, it uses the base Person's override. So printing emp calls Person's ToString, producing 'Person: Carol, 28'.
❓ Predict Output
advanced2:00remaining
Deconstruction behavior in record inheritance
What is the output of this C# code demonstrating deconstruction in record inheritance?
C Sharp (C#)
public record Person(string Name, int Age) { public void Deconstruct(out string name, out int age) => (name, age) = (Name, Age); } public record Employee(string Name, int Age, string Position) : Person(Name, Age) { public void Deconstruct(out string name, out int age, out string position) => (name, age, position) = (Name, Age, Position); } var emp = new Employee("Dave", 40, "Lead"); var (n, a, p) = emp; Console.WriteLine($"{n}, {a}, {p}");
Attempts:
2 left
💡 Hint
The derived record defines a Deconstruct method with three out parameters matching its properties.
✗ Incorrect
The Employee record defines a Deconstruct method with three parameters, so deconstruction extracts all three values correctly. The output prints all three values separated by commas.
🧠 Conceptual
expert2:00remaining
Understanding record inheritance and immutability
Which statement about record inheritance and immutability in C# is correct?
Attempts:
2 left
💡 Hint
Think about how records handle properties and immutability in inheritance.
✗ Incorrect
Records provide immutability by default for properties declared with init-only setters. Derived records can add new properties, including mutable ones, but this does not change the base record's properties. So immutability applies per property, not enforced globally.