Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Record inheritance in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Record inheritance
Define base record
↓
Define derived record : base
↓
Create instance of derived record
↓
Access base and derived properties
↓
Use with-expressions to copy and modify
↓
Print or compare records
Shows how a derived record inherits properties and behavior from a base record, allowing creation, access, and modification.
Execution Sample
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);
Defines a base record Person and a derived record Employee, creates an Employee instance, and prints it.
Execution Table
StepActionEvaluationResult
1Define record Person with Name and AgePerson record createdPerson(Name, Age)
2Define record Employee inheriting Person, adds PositionEmployee record createdEmployee(Name, Age, Position) : Person(Name, Age)
3Create Employee instance emp with Name='Alice', Age=30, Position='Developer'emp = new Employee("Alice", 30, "Developer")emp.Name = 'Alice', emp.Age = 30, emp.Position = 'Developer'
4Print empConsole.WriteLine(emp)Employee { Name = Alice, Age = 30, Position = Developer }
5Use with-expression to create emp2 from emp with Position='Manager'var emp2 = emp with { Position = "Manager" }emp2.Name = 'Alice', emp2.Age = 30, emp2.Position = 'Manager'
6Print emp2Console.WriteLine(emp2)Employee { Name = Alice, Age = 30, Position = Manager }
7Compare emp and emp2emp == emp2False
8Compare emp with itselfemp == empTrue
💡 All steps executed; program ends after printing and comparisons.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
empnull{Name: 'Alice', Age: 30, Position: 'Developer'}{Name: 'Alice', Age: 30, Position: 'Developer'}{Name: 'Alice', Age: 30, Position: 'Developer'}
emp2nullnull{Name: 'Alice', Age: 30, Position: 'Manager'}{Name: 'Alice', Age: 30, Position: 'Manager'}
Key Moments - 3 Insights
Why does Employee inherit Name and Age even though they are not declared again?
Because Employee inherits from Person, it automatically has the Name and Age properties defined in Person, as shown in steps 1 and 2 of the execution_table.
What does the 'with' expression do in step 5?
It creates a new Employee record copying all properties from emp but changes Position to 'Manager', as shown in step 5 of the execution_table.
Why are emp and emp2 not equal even though they share Name and Age?
Because Position differs ('Developer' vs 'Manager'), the records are not equal, as shown in step 7 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the values of emp's properties?
AName='Alice', Age=30, Position='Developer'
BName='Alice', Age=30, Position='Manager'
CName='Bob', Age=25, Position='Developer'
DName='Alice', Age=25, Position='Developer'
💡 Hint
Check the 'Result' column in step 3 of the execution_table.
At which step does the 'with' expression create a new record with a changed Position?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for the step describing 'with-expression' in the execution_table.
According to variable_tracker, what is the value of emp2 after step 5?
A{Name: 'Alice', Age: 30, Position: 'Developer'}
B{Name: 'Alice', Age: 30, Position: 'Manager'}
Cnull
D{Name: 'Bob', Age: 30, Position: 'Manager'}
💡 Hint
Check the 'After Step 5' column for emp2 in variable_tracker.
Concept Snapshot
Record inheritance in C#:
- Use 'record Derived(...) : Base(...)' syntax
- Derived inherits properties from Base
- Create instances with all properties
- Use 'with' to copy and modify
- Records compare by value automatically
Full Transcript
This example shows how record inheritance works in C#. We define a base record Person with Name and Age. Then we define Employee that inherits Person and adds Position. When we create an Employee instance, it has all three properties. Printing the record shows all properties. Using the 'with' expression creates a new record copying existing values but changing specified ones. Comparing records checks all properties for equality. This trace helps understand how inheritance and value equality work with records.

Practice

(1/5)
1.

What does record inheritance in C# allow you to do?

easy
A. Delete properties from the base record in the derived record
B. Change the data type of existing properties in a record
C. Convert a record into a class automatically
D. Create a new record that extends an existing record with additional properties

Solution

  1. Step 1: Understand record inheritance purpose

    Record inheritance lets you build new records based on existing ones, adding new properties while keeping the base properties.
  2. Step 2: Analyze options

    Only Create a new record that extends an existing record with additional properties correctly describes extending an existing record with new properties. Options A, B, and D describe actions not supported by record inheritance.
  3. Final Answer:

    Create a new record that extends an existing record with additional properties -> Option D
  4. Quick Check:

    Record inheritance = extend with new properties [OK]
Hint: Inheritance means adding new features, not removing or changing [OK]
Common Mistakes:
  • Thinking inheritance changes base properties
  • Confusing records with classes
  • Assuming inheritance deletes base properties
2.

Which of the following is the correct syntax to inherit a record Person in C#?

public record Person(string Name, int Age);
easy
A. public record Employee(string Name, int Age, string Position) : Person(Name, Age);
B. public record Employee : Person(string Name, int Age, string Position);
C. public record Employee(string Name, int Age, string Position) inherits Person(Name, Age);
D. public record Employee(string Name, int Age, string Position) extends Person(Name, Age);

Solution

  1. Step 1: Recall record inheritance syntax

    In C#, a derived record uses colon : to inherit and passes base parameters in parentheses after the base name.
  2. Step 2: Check each option

    public record Employee(string Name, int Age, string Position) : Person(Name, Age); correctly uses colon and passes base parameters. Options B, C, and D use incorrect keywords or syntax.
  3. Final Answer:

    public record Employee(string Name, int Age, string Position) : Person(Name, Age); -> Option A
  4. Quick Check:

    Use ':' and pass base params in parentheses [OK]
Hint: Use ':' and pass base parameters in parentheses [OK]
Common Mistakes:
  • Using 'inherits' or 'extends' keywords
  • Not passing base parameters
  • Placing base parameters incorrectly
3.

What will be the output of this code?

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.Name + ", " + emp.Age + ", " + emp.Position);
medium
A. Alice, 30
B. Developer, 30, Alice
C. Alice, 30, Developer
D. Compilation error

Solution

  1. Step 1: Understand record inheritance and properties

    The Employee record inherits Name and Age from Person and adds Position. Creating Employee sets all three properties.
  2. Step 2: Analyze output statement

    The code prints emp.Name, emp.Age, and emp.Position separated by commas, so output is "Alice, 30, Developer".
  3. Final Answer:

    Alice, 30, Developer -> Option C
  4. Quick Check:

    Inherited + new properties print correctly [OK]
Hint: Inherited properties are accessible like own properties [OK]
Common Mistakes:
  • Assuming only base or only derived properties print
  • Expecting compilation error due to inheritance
  • Mixing property order in output
4.

Identify the error in this record inheritance code:

public record Person(string Name, int Age);
public record Employee(string Name, int Age, string Position) : Person;

var emp = new Employee("Bob", 25, "Tester");
medium
A. Employee record cannot have more than two properties
B. Missing base constructor arguments in Employee inheritance
C. Position property must be in base record
D. Cannot inherit from a record

Solution

  1. Step 1: Check inheritance syntax

    Employee inherits Person but does not pass required base constructor arguments (Name, Age) after Person.
  2. Step 2: Identify correct fix

    Base constructor arguments must be passed like : Person(Name, Age). Omitting them causes a compilation error.
  3. Final Answer:

    Missing base constructor arguments in Employee inheritance -> Option B
  4. Quick Check:

    Base constructor args required after ':' [OK]
Hint: Always pass base constructor parameters after ':' [OK]
Common Mistakes:
  • Forgetting to pass base parameters
  • Thinking inheritance disallows extra properties
  • Assuming records can't inherit
5.

You have a base record Vehicle(string Make, string Model). You want to create a derived record Car that adds int Doors and overrides the ToString() method to show all info. Which is the correct way to do this?

hard
A. public record Car(string Make, string Model, int Doors) : Vehicle(Make, Model) { public override string ToString() => $"{Make} {Model} with {Doors} doors"; }
B. public record Car : Vehicle { public int Doors; public override string ToString() => $"{Make} {Model} with {Doors} doors"; }
C. public record Car(string Make, string Model, int Doors) inherits Vehicle { public override string ToString() => $"{Make} {Model} with {Doors} doors"; }
D. public record Car(string Make, string Model, int Doors) extends Vehicle(Make, Model) { public override string ToString() => $"{Make} {Model} with {Doors} doors"; }

Solution

  1. Step 1: Use correct inheritance syntax with parameters

    Derived record Car must inherit Vehicle using colon and pass Make and Model parameters: : Vehicle(Make, Model).
  2. Step 2: Override ToString() properly

    Inside Car, override ToString() with the correct string interpolation including Doors property.
  3. Step 3: Check options

    public record Car(string Make, string Model, int Doors) : Vehicle(Make, Model) { public override string ToString() => $"{Make} {Model} with {Doors} doors"; } uses correct syntax and override. Options B, C, and D use invalid keywords or omit constructor parameters.
  4. Final Answer:

    public record Car(string Make, string Model, int Doors) : Vehicle(Make, Model) { public override string ToString() => $"{Make} {Model} with {Doors} doors"; } -> Option A
  5. Quick Check:

    Use ':' with base params and override method inside record [OK]
Hint: Use ':' with base params and override inside record body [OK]
Common Mistakes:
  • Using 'inherits' or 'extends' keywords
  • Not passing base constructor parameters
  • Trying to add fields instead of properties
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard