Search⌘ K

Solution Review: Implement the Parametrized Constructor

Explore how to implement parametrized constructors in C# by using the base keyword to invoke the base class constructor. Understand the process of initializing derived class objects with specific parameters to enhance your mastery of inheritance concepts.

We'll cover the following...

Solution

C#
// Base Class
class Laptop {
// Private Data Members
public string Name{get; set;}
public Laptop() { // Default Constructor
}
public Laptop(string name) { // Default Constructor
this.Name = name;
}
}
// Derived Class
class Dell : Laptop {
public Dell(string name)
: base(name)
{
}
}
class Demo {
public static void Main(string[] args) {
Dell dell = new Dell("Dell Inspiron");
Console.WriteLine(dell.Name);
}
}

Explanation

...