Understand Constructor and Types of Constructor in C# with examples
- By Gurunatha Dogi in C#
- Jun 9th, 2013
- 38744
- 1
What is a constructor?
Constructor is a method which gets executed automatically when we create or instantiate object of that class having constructor.
More Highlights of Constructor
A single class can have multiple constructors means we can have more than one constructor in a class. It is also called as overloaded constructor.
A benefit of using a constructor is that it guarantees that the object will go through a proper initialization before an object being used means we can pre-initialize some of the class variables with values before an object being used.
Constructors can either be static constructor or an instance constructor.
Constructors do not have any return types means. So there is no return type.
A constructor can be called another constructor by using "this" keyword. "this" keyword is the current instance of a class.
Syntax of a Constructor
Constructors are declared by using the same class name without any return type. An example is shown below.
class clsAddition{ public clsAddition(){ //Code goes here } }
Types of Constructors in C#
There are 5 types of constructor in C# as listed below
Default Constructor
Parameterized Constructor
Copy Constructor
Static Constructor
Private Constructor
Default Constructor
A constructor without any parameters is called as default constructor means a constructor which does not have any input parameter is known as default constructor.
Example of Default Constructor
public class clsAddition{ public int Mycode = 0; public clsAddition(){ Mycode = 9; } } class Program{ static void Main(string[] args){ clsAddition obj = new clsAddition(); Consolw.WriteLine("Code value is : {0}",obj.Mycode); } }
Parameterized Constructor
A constructor having one or more parameters is called as parameterized constructor means a constructor which is having single input parameter or multiple input parameters of same data types or different data types are known as parameterized constructor.
Example of Parameterized Constructor
public class clsAddition{ public string itype = ""; public int x = 0; public int y = 0; public clsAddition(string type, int a, int b){ itype = type; x = a; y = b; } } class Program{ static void Main(string[] args){ clsAddition obj = new clsAddition("Addition",7,2); Consolw.WriteLine("Type is : {0}",obj.itype); Consolw.WriteLine("Variable 1 value is : {0}",obj.x); Consolw.WriteLine("Variable 2 value is : {0}",obj.y); } }
Copy Constructor
A constructor that contains a parameter of same class type is called as copy constructor.
C# does not provide a copy constructor. A copy constructor enables you to copy the data stored in the member variables of an object of the class into another new object means it helps to copy data stored in one object into another new object of the same instance.
Example of Copy Constructor
class Car { public string _nameofcar = ""; public int _carno = 0; public double _carprice = 0; public Car(string NameofCar, int CarNumber, double CarPrice) { _nameofcar = NameofCar; _carno = CarNumber; _carprice = CarPrice; } //Copy Constructor public Car(Car objCar) { _nameofcar = objCar._nameofcar; _carno = objCar._carno; _carprice = objCar._carprice; } } class Program { static void Main(string[] args) { Car obj1 = new Car("BMW Sedan", 4545, 5000000); Car obj2 = new Car(obj1); Console.WriteLine("Car Name : {0}", obj2._nameofcar); Console.WriteLine("Car Number : {0}", obj2._carno); Console.WriteLine("Car Price : {0}", obj2._carprice); } }
Static Constructor
Static constructor should be parameter less means it should not contain any input parameter. Program will not execute if static constructor is having any input parameter.
Static constructor can be invoked once for any number instances are created and it is invoked only during the first initialization of instance. It is used to initialize static fields of the class
Static constructor is created using a static keyword as shown below.
class clsAddition{ static clsAddition(){ ….Code goes here } }
Example of Static Constructor
public class clsAddition { private int a = 0; private int b = 0; public static int _counter; public clsAddition(int x, int y) { a = x; b = y; } static clsAddition() { Console.WriteLine("Static constructor is called"); _counter++; Console.WriteLine("Counter Value is {0}", _counter); } public clsAddition() { a = 150; b = 160; } public void Display() { Console.WriteLine("Addition Value is {0}", a + b); } } class Program { static void Main(string[] args) { clsAddition objadd = new clsAddition(); clsAddition._counter = 9; objadd.Display(); Console.WriteLine("\nSecond time calling a constructor \n"); objadd = new clsAddition(10, 15); objadd.Display(); } }
If you see in this example we have created instances 2 times and at a same time we tried to change the counter value but on the output if you see it is showing the same value as it was assigned during the first initialization process in the static constructor.
See the output
So it is concluded that static constructor can be invoked once for any number instances are created and it is invoked only during the first initialization process.
Private Constructor
A constructor with "private" access modifier in a class is called as private constructor.
A class with private constructor cannot be inherited.
We cannot create an object of the class which is having a private constructor. Program will not allow us to create an object of a class having private constructor.
A long with private constructor we can have a public constructor (overloaded constructor but not of the same type).
To access the methods or properties in a class with private constructor we can assign methods or properties with "static" keyword.
public class clsEmployee { public static int emplcounter = 9; private clsEmployee() { } } class Program { static void Main(string[] args) { Console.WriteLine("Counter value" + clsEmployee.emplcounter); } }
So this is all about the constructors in C#. If you have any doubt or query kindly let me know through your comments and if you like this post kindly share it with your friends.
Gurunatha Dogi
Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more
Comments
By Suman bhardwaj on 2014-08-23
very niceAdd a Comment

- By Gurunatha Dogi
- May 21st, 2013
- 108199
- 1
OOPS Principle - Polymorphism in C# with an example

- By Gurunatha Dogi
- May 17th, 2013
- 88725
- 1
OOPS Principle - Inheritance in C# with an example

- By Gurunatha Dogi
- May 3rd, 2013
- 124878
- 1
OOPS Principle - Encapsulation in C# with an example

- By Gurunatha Dogi
- Apr 30th, 2013
- 56126
- 1