Calling the base constructor in C#
To call base constructor in C# we can have colon ":
" followed by base()
method at constructor level of child class, then you will need to pass any required parameters from parent class. using System;
public class Person {
public Person(string personName) {
Console.WriteLine("Printing person name from base constructor {0}", personName);
}
}
public class Student : Person {
public Student(string studentName) : base(studentName) {
Console.WriteLine("Printing person name from object {0}", studentName);
}
}
public class Program
{
public static void Main(string[] args)
{
Student student = new Student("William");
}
}
Output
Printing person name from base constructor William
Printing person name from object William
Printing person name from object William