C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In this tutorial, we'll discuss the relationship between constructors in C# inheritance. Constructors are special member functions in a class that are automatically called when an object of that class is created. Inheritance is the process by which one class (the derived class) inherits the properties and methods of another class (the base class).
A constructor is a method that has the same name as the class and is used to initialize an object's data members. It can have any access modifier (public, private, protected, or internal), and can be parameterized or non-parameterized.
Example of a constructor:
public class Person { public string Name { get; set; } public Person(string name) { Name = name; } }
Inheritance is a way to create a new class that is a modified version of an existing class. The new class (the derived class) inherits the members of the existing class (the base class). The derived class can have additional members as well.
Example of inheritance:
public class Employee : Person { public int EmployeeID { get; set; } public Employee(string name, int employeeID) : base(name) { EmployeeID = employeeID; } }
When a derived class is created, the constructor of the base class is called first, followed by the constructor of the derived class. This ensures that the base class is properly initialized before the derived class.
In the above example, the constructor of the Employee class is defined with a : base(name)
call, which indicates that the base class constructor should be called with the name
parameter. This initializes the Name
property in the Person class, and then the derived class constructor continues to initialize the EmployeeID
property.
base
KeywordThe base
keyword is used in the derived class constructor to call the base class constructor explicitly. It allows you to pass parameters from the derived class constructor to the base class constructor.
Example:
public class Manager : Employee { public string Department { get; set; } public Manager(string name, int employeeID, string department) : base(name, employeeID) { Department = department; } }
In this tutorial, we have discussed the relationship between constructors in C# inheritance. Constructors play a crucial role in initializing objects, and when working with inheritance, it's important to ensure that both the base and derived class constructors are called in the correct order.
Remember to use the base
keyword when calling the base class constructor from a derived class constructor, and ensure that all necessary properties and fields are properly initialized.
Base class constructor in C# inheritance:
public class Animal { public Animal() { Console.WriteLine("Base class constructor called."); } }
Derived class constructor in C# inheritance:
public class Dog : Animal { public Dog() { Console.WriteLine("Derived class constructor called."); } }
Passing parameters to the base class constructor in C#:
public class Animal { public Animal(string name) { Console.WriteLine($"Animal named {name} created."); } } public class Dog : Animal { public Dog(string name) : base(name) { Console.WriteLine($"Dog named {name} created."); } }
Overriding constructors in C# inheritance:
public class Animal { public Animal() { Console.WriteLine("Base class constructor called."); } } public class Dog : Animal { public Dog() : base() { Console.WriteLine("Derived class constructor called."); } }