In C#, a constructor is a special method used to initialize an object of a class. It is called when an instance of the class is created using the new keyword. Constructors allow you to set up the object with initial values, or perform any setup tasks when the object is created.
Characteristics of Constructors
1. Same Name as the Class: The constructor’s name must be the same as the class name.
2. No Return Type: Constructors do not return any value, not even void.
3. Automatically Called: When a new instance of a class is created, the constructor is automatically invoked.
4. Cannot Be Inherited: Constructors cannot be inherited, but they can be overloaded.
Types of Constructors
There are mainly three types of constructors in C#
- Default Constructor
- Parameterized Constructor
- Static Constructor
1. Default Constructor
A default constructor is a constructor that takes no parameters. If you don’t provide any constructor in your class, C# provides a default constructor for you, initializing fields to their default values.
Example:
using System;
public class Employee
{
public string name;
public int age;
// Default constructor
public Employee()
{
name = "John";
age = 35;
}
public void displayInfo()
{
Console.WriteLine($"empName: {name}, empAge: {age}");
}
}
class Program
{
static void Main(string[] args)
{
// Create an object using the default constructor
Employee emp1 = new Employee();
emp1.displayInfo(); // Output: empName: John, empAge: 35
}
}
Output:
Explanation:
- The Employee class has a default constructor that sets name to “John” and age to 35.
- This constructor is automatically invoked when you create an instance of Employee.
Parameterized Constructor
A parameterized constructor is a constructor that takes one or more arguments. This allows you to initialize objects with specific values when creating them.
Example:
using System;
public class Employee
{
public string name;
public int age;
// Default constructor
public Employee(string empName, int empAge)
{
name = empName;
age = empAge;
}
public void displayInfo()
{
Console.WriteLine($"empName: {name}, empAge: {age}");
}
}
class Program
{
static void Main(string[] args)
{
// Create an object using the default constructor
Employee emp1 = new Employee("John", 35);
emp1.displayInfo(); // Output: empName: John, empAge: 35
}
}
Output:
Explanation:
The Employee class now has a parameterized constructor that allows the name and age properties to be set when an object is created.
Static Constructor
A static constructor is used to initialize static members of a class. It is called once, before any instance of the class is created or any static members are accessed. A static constructor does not take parameters.
Example:
using System;
public class Employee
{
public static string name;
public int age;
// static constructor
static Employee()
{
name = "John";
Console.WriteLine("Static Constructor called");
}
// Default constructor
public Employee()
{
Console.WriteLine("Instance Constructor called");
}
public void displayInfo()
{
Console.WriteLine($"empName: {name}");
}
}
class Program
{
static void Main(string[] args)
{
// Create an object using the default constructor
Employee emp1 = new Employee();
emp1.displayInfo(); // Output: empName: John, empAge: 35
}
}
Output:
Instance Constructor called
empName: John
Explanation:
- The name is a static field that is initialized in the static constructor.
- The static constructor is called only once, no matter how many instances of the class are created.
- The static constructor is automatically called before any instance constructor.
Parent Class Constructor call from Child Class
In C#, you can call the parent class constructor from a child class using the base keyword. This allows you to initialize the parent class before the child class’s constructor code is executed.
Example:
using System;
class Employee
{
// Parent class (Employee) constructor
public Employee(string message)
{
Console.WriteLine("Parent class constructor called, "+ message);
}
}
class Manager : Employee
{
// Child class (Manager) constructor
public Manager(string message) : base(message) // Call the Parent class constructor
{
Console.WriteLine("Child class constructor called.");
}
}
class ProgramExample
{
static void Main()
{
// Create an object of the Manager class
Manager manager = new Manager("Hello World!");
}
}
Output:
Child class constructor called.
Explanation:
- Employee Class: The Employee class has a constructor that takes a string parameter.
- Manager Class: The Manager class inherits from Employee. In the Manager class constructor, the base(message) keyword is used to call the constructor of the Employee class.
- base Keyword: It ensures that the parent class constructor is invoked first, with the message argument passed from the child class’s constructor.