Introduction
An instance constructor is a method whose task is to create an instance of a class. A constructor is a member function whose task is to initialize the objects of its class, in other words it is a method of a class that executes when the class's objects are created. It is called a constructor because it initializes the data members of the class.
Creating a Constructor in C#
- The Constructor name is the same as the class name.
- A Constructor does not have a return type.
- Multiple overloads of constructors can be defined.
- Constructors can have an access modifier.
- Constructors are mainly used to initialize private fields of the class.
- If we do not provide a constructor for our object, C# will create one by default that instantiates the object and sets member variables to their default values.
- The constructor is invoked by the new operator immediately after memory is allocated for the new object.
- The class can have only one default constructor.
- Structs cannot contain an explicit default constructor because one is provided automatically by the compiler. This constructor initializes each field in the struct to the default values.
Instance Constructors in C#
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Private Constructor
Now let's explain each in detail.
1. Default Constructor in C#
A constructor that has no parameters is called the default constructor. Each class has a default constructor. When we don't want to initialize any values for class members, then there is no need to define it for the class, but when we want to initialize values of class members, then we need to provide it for the class. When we don't create a constructor in the class, then the compiler will automatically create a default constructor in the class that initializes all fields by their default values.
It has a drawback that when we create instances of the class, then every instance member will be assigned the same values.
Example. Create a default constructor for the Employee class that assigns a value of salary and age member variables for an Employee class.
class Employee
{
private int salary = 100;
private int age = 18;
public Employee()
{
salary = 1000;
age = 21;
}
}
2. Parameterized Constructor in C#
A Constructor that has at least one parameter is called a parameterized constructor. A class can have multiple parameterized constructors. Each parameterized constructor is separated by the parameter types and the number of parameters. By using a parameterized constructor we can assign different values to each instance of the class.
Example. Create a parameterized constructor for the Employee class that can be used to assign different values of salary and age member variables for the Employee class according to the instance to be created.
class Employee
{
private int salary = 100;
private int age = 18;
public Employee(int salary, int age)
{
this.salary = salary;
this.age = age;
}
}
3. Copy Constructor in C#
A Copy Constructor is a parameterized constructor that has a parameter of the same class type, in other words the parameter has an instance of the same class. It's main purpose is to initialize a new instance with the same values as an existing instance.
Example. Create a parameterized constructor for the Employee class that has an instance of the Employee class to assign the same values of salary and age member variables of another Employee class to the new instance.
public class Employee {
private int salary = 100;
private int age = 18;
public Employee(Employee previousEmployee) {
salary = previousEmployee.salary;
age = previousEmployee.age;
}
}
4. Private Constructor in C#
We can create a constructor as private. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes and the same class) are not allowed to create instances of this class.
- A Private constructor does not have any parameters.
- The declaration of the private constructor is empty.
- It has a private modifier; by default, a constructor has a private modifier.
- In a class, we can declare only one private constructor.
Example. The Employee class has a private constructor in the following.
public class Employee {
private Employee() {
// Private constructor
}
}
Implementation of constructor
Create a class employee that has two constructors; one is a default constructor, and another is a parameterized constructor. Both constructors initialize the member field of the class. See.
public class Employee {
private int salary = 100;
public Employee() {
salary = 1000;
}
public Employee(int salary) {
this.salary = salary;
}
public int getSalary() {
return salary;
}
public void setSalary(int value) {
salary = value;
}
}
Create a program that creates instances of an Employee class and calls the salary field that is updated by constructors.
using System;
namespace ConstructorExample
{
class Program
{
static void Main(string[] args)
{
// Call default constructor
Employee objEmployee1 = new Employee();
Console.WriteLine("objEmployee1 Salary is {0}", objEmployee1.Salary);
// Call parameterized constructor
int salary = 500;
Employee objEmployee2 = new Employee(salary);
Console.WriteLine("objEmployee2 Salary is {0}", objEmployee2.Salary);
salary = 800;
Employee objEmployee3 = new Employee(salary);
Console.WriteLine("objEmployee3 Salary is {0}", objEmployee3.Salary);
Console.Read();
}
}
}