0
0
C Sharp (C#)programming~20 mins

Record inheritance in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Record Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AEmployee { Name = Alice, Age = 30, Position = Developer }
BPerson { Name = Alice, Age = 30 }
CPerson { Name = Alice, Age = 30, Position = Developer }
DEmployee { Name = Alice, Position = Developer }
Attempts:
2 left
💡 Hint
Remember that the derived record includes all properties from base and derived types in its ToString output.
Predict Output
intermediate
2: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);
AFalse
BCompilation error
CRuntime exception
DTrue
Attempts:
2 left
💡 Hint
Records compare by value, including all properties from base and derived records.
Predict Output
advanced
2: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);
ACompilation error due to ToString override
BPerson: Carol, 28
CPerson { Name = Carol, Age = 28 }
DEmployee { Name = Carol, Age = 28, Position = Manager }
Attempts:
2 left
💡 Hint
The derived record inherits the overridden ToString from the base record unless it overrides it again.
Predict Output
advanced
2: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}");
ADave, 40, Lead
BDave, 40
CCompilation error: Deconstruct mismatch
DRuntime error: Deconstruct failed
Attempts:
2 left
💡 Hint
The derived record defines a Deconstruct method with three out parameters matching its properties.
🧠 Conceptual
expert
2:00remaining
Understanding record inheritance and immutability
Which statement about record inheritance and immutability in C# is correct?
ADerived records must override all base record properties to maintain immutability.
BRecords are always fully immutable, so derived records cannot add mutable properties.
CDerived records can add mutable properties without affecting the base record's immutability.
DRecords do not support inheritance to preserve immutability.
Attempts:
2 left
💡 Hint
Think about how records handle properties and immutability in inheritance.