Constructors in C#



A constructor is a special method available under every class responsible for initializing variables of the class.

The name of the constructor method will be the same as the class name and the method does not return a value.

Every class requires a constructor for execution and if not present the class can't be executed so if the programmer did not define a constructor under his class then the compiler defines a constructor for the class while compiling.

We can also define our own constructor in a class as follows:

[ <modifiers>] name ([ <param definations>])
{
Statements
}

Class Condemo

class Condemo()
{
Console.writeline("constructor");
}
Void demo
{
Console.writeline("method");
}

static void main()
{
Condemo cd1=new Condemo();
Condemo cd2=new Condemo();
Condemo cd3=cd2;
Cd1.demo();cd2.demo();cd3.demo();
Console.readline();
}}

When we execute the above class the first statement under constructor condemo() will execute thrice and then all remaining operations are executed.

The constructor are of two types

  1. Default or zero argument constructor
  2. Parametertised constructor

A constructor without a parameter is known as the default constructor. This can be defined either by the programmer or will be defined automatically by the compiler when there is not a constructor in a class.

A constructor with a input parameter is known as a parameterized constructor. This can be defined by the programmer.

If the constructor is parameterized then we need to provide values for the parameter(s) when creating an object of the class; to try a parameterized constructor, using our previous class condemo is as follows:

Class condemo

Condemo(int x)
{
Console.writeline("constructor" +x);
}

Now in the main method, values are passed to the constructor when creating it's object as follows:
Condemo cd1=new condemo(10);
Condemo cd1=new condemo(20);

Add class conparams.cs and write the following:

Class conparams
{
Int x,y;
Conparams(intx,int y)
{
This.x=x; this.y=y
}

Void add()
{
Console.writeline(x+y);
}

Void sub()
{
Console.writeline(x-y);
}

Void mul()
{
Console.writeline(x*y);
}

Void div
{
Console.writeline(x/y);
}

Static void main()
{
Conparams p =new conparams(100,200)
p.add();p.sub();p.mul();p.div();
console.readline();
}

The above program shows that whenever a classs contains multiple methods that have to be executed using a set of vaues, in such cases it will be better if the values are provided in a constructor instead of methods.

Let's see one more example.
class Customer
{double bal;

Customer(int customerid)//constructor
{
Bal=<load data from database for given id
}

Double getbal
{
Return bal;
}
Void deposit
{
Bal+=amt;
}
Void withdraw
{
Bal-=amt;
}
}

In the above class, the variable bal in the class gets initialized in the constructor with a key value sent through a parameterized constructor.

There the value will be loaded for balance from database which can be as following:

Cust id cust name bal
101 xxx 5000
102 yyy 8787
103 ccc 7777

Now for each object we create the class customer by providing a customer id for maintaining a copy of a balance.

Ex:

Customer c1 =new customer(101);
Customer c1 =new customer(102);

Customer c1 =new customer(103);

Now transactions performed by customer using his object will perform changes on variable balance which was associated with it without reflecting changes to others.


Similar Articles