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

Record inheritance in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a record named Person.

C Sharp (C#)
public record [1](string Name, int Age);
Drag options to blanks, or click blank then click option'
Ainterface
Bclass
Cstruct
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using class or struct instead of record.
Forgetting to name the record.
2fill in blank
medium

Complete the code to make Employee inherit from Person record.

C Sharp (C#)
public record Employee(string Name, int Age, string Position) : [1](Name, Age);
Drag options to blanks, or click blank then click option'
Aobject
BEmployee
CPerson
Drecord
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong base record name.
Omitting the base constructor call.
3fill in blank
hard

Fix the error in the code to correctly override the ToString method in the Employee record.

C Sharp (C#)
public record Employee(string Name, int Age) : Person(Name, Age)
{
    public override string [1]() => $"Employee: {Name}, {Age}";
}
Drag options to blanks, or click blank then click option'
AToString
BToStr
CtoString
DPrint
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase toString which does not exist in C#.
Using incorrect method names like ToStr or Print.
4fill in blank
hard

Fill both blanks to create a derived record Manager that inherits from Employee and adds a Department property.

C Sharp (C#)
public record Manager(string Name, int Age, string Position, string [1]) : Employee(Name, Age, Position)
{
    public string [2] { get; init; } = [1];
}
Drag options to blanks, or click blank then click option'
ADepartment
BPosition
CAge
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the property and initializer.
Using base record property names instead of the new property.
5fill in blank
hard

Fill all three blanks to override the PrintDetails method in Manager, calling base method and adding department info.

C Sharp (C#)
public record Manager(string Name, int Age, string Position, string Department) : Employee(Name, Age, Position)
{
    public void PrintDetails()
    {
        base.[1]();
        Console.WriteLine($"Department: [2]");
        Console.WriteLine($"Position: [3]");
    }
}
Drag options to blanks, or click blank then click option'
APrintDetails
BDepartment
CPosition
DToString
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a wrong base method name.
Using incorrect property names in the output.