In C#, as we know we have to create the object for Class to call a method or to assign values to fields. Today we discuss about different ways to assign values to fields in a class. Generally we assign the values to fields after creating the object as shown below.
public class Employee
{
public int iId;
public string sName;
}
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee();
obj.iId = 10;
obj.sName = "A";
}
}
As we see we are assigning the values to Employee class fields through the object of it. Here we are assigning the values by initialising each field, this is not a problem if we have limited number of fields. For example if we have 20 number of variables, it will take 20 lines to assign values to fields. To avoid this C# provides Constructor method where we can assign values to fields while creating the object itself.
Before going to deep into Constructors remember one thing, C# creates default constructor internally whenever you are creating the object for class which ensures allocating the memory for object and to set the default values for all fields within the class. Constructor is a special method which is called indirectly whenever you are creating the object by using new keyword. Here one more point you have to remember is constructor never returns any value like method.
public class Employee
{
public int iId;
public string sName;
//default constructor
public Employee()
{
}
}
As shown above you will have the default constructor if you won't define any custom constructor. Default constructor is created by run-time while creating the object. Default constructor won't take any parameter as input.
If you want you can define your own custom constructor as shown below.
public class Employee
{
public int iId;
public string sName;
//custom constructor
public Employee()
{
iId = 10;
sName = "A";
}
}
Parameterized Constructors:
We can pass parameters to custom constructor. That means we can set the field values while creating the object itself. If the constructor is taking parameter, that constructor is called as custom constructor.
We can create the custom constructor as shown below by taking the same Employee example.
public class Employee
{
public int iId;
public string sName;
//Parameterized constructor
public Employee(int iEmpId, string sEmpName)
{
iId = iEmpId;
sName = sEmpName;
}
}
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee(10, "A");
}
}
As shown above we are setting the Employee class field values while creating the object itself through the parameterized constructor.
Constructor Overloading:
Like method overloading we have constructor overloading also, that means we can multiple methods with different signature(differ in number of parameters or type of parameters). Depending the parameters passing while creating the object corresponding constructor will call.
Let's have Employee class with multiple constructors as shown below.
public class Employee
{
public int iId;
public string sName;
public Employee()
{
iId = 11;
sName = "B";
}
public Employee(int iEmpId)
{
iId = iEmpId;
sName = "B";
}
public Employee(string sEmpName)
{
iId = 11;
sName = sEmpName;
}
public Employee(int iEmpId, string sEmpName)
{
iId = iEmpId;
sName = sEmpName;
}
}
As shown above we have four constructors. First constructor does not take any parameters, second constructor takes only one parameter of int type, third constructor also takes only one parameter but string type and finally fourth parameter takes two parameters of type int, string.
Now we observer which constructor will call by creating the objects as shown below.
class Program
{
static void Main(string[] args)
{
Employee obj1 = new Employee();
Employee obj2 = new Employee(10);
Employee obj3 = new Employee("A");
Employee obj4 = new Employee(10, "A");
}
}
Here we created the four objects. while creating obj1 - parameter less constructor will execute, while creating obj2 - constructor which takes only one parameter which is int type will execute, while creating obj3 - constructor which takes only one parameter which is string type will execute and finally while creating the obj4 - constructor which takes two parameters one is int type, another is string type will execute.