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

Record inheritance in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Record inheritance
📖 Scenario: You are creating a simple program to manage information about vehicles. You want to use C# records to store data about a general vehicle and a more specific type of vehicle, a car.
🎯 Goal: Build a small program that defines a base record Vehicle and a derived record Car that inherits from Vehicle. Then create an instance of Car and display its details.
📋 What You'll Learn
Define a record called Vehicle with properties Make and Year.
Define a record called Car that inherits from Vehicle and adds a property Model.
Create an instance of Car with specific values for Make, Year, and Model.
Print the details of the Car instance.
💡 Why This Matters
🌍 Real World
Record inheritance helps organize related data types in programs, like different kinds of vehicles, while sharing common properties.
💼 Career
Understanding record inheritance is useful for writing clean, maintainable code in C# applications, especially in domains like software modeling and data handling.
Progress0 / 4 steps
1
Create the base record Vehicle
Write a record called Vehicle with two properties: string Make and int Year. Use positional parameters in the record definition.
C Sharp (C#)
Need a hint?

Use the syntax public record Vehicle(string Make, int Year); to create a record with two properties.

2
Create the derived record Car
Write a record called Car that inherits from Vehicle. Add a new property string Model using positional parameters. Make sure Car calls the base record constructor for Make and Year.
C Sharp (C#)
Need a hint?

Use public record Car(string Make, int Year, string Model) : Vehicle(Make, Year); to inherit and add a property.

3
Create an instance of Car
Create a variable called myCar and assign it a new Car object with Make as "Toyota", Year as 2022, and Model as "Corolla".
C Sharp (C#)
Need a hint?

Use Car myCar = new Car("Toyota", 2022, "Corolla"); to create the object.

4
Print the Car details
Write a Console.WriteLine statement to print the myCar object. This will show all property values.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(myCar); inside a Main method to print the record.