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

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

Choose your learning style9 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.