Define Constructors in C Sharp with Example and Types of Constructor in C# with Example

Inntroduction:-


1.Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created.

2.They are responsible for object initialization and memory allocation of its class. 

3.If we create any class without constructor, the compiler will automatically create one default constructor for that class.

4.There is always at least one constructor in every class.

5.In class there can any number of constructor in class 

6.Constructor does not have  need any return type ,not even void too

7.In class there can be one static constructor 

8.Constructor of class must be same as the  class name


Types of Constructors



Basically constructors are 5 types those are



      1.    Default Constructor

      2.    Parameterized Constructor

      3.    Copy Constructor

      4.    Static Constructor

      5.    Private Constructor

Default Constructor


using System;
namespace ConsoleApplication3
{
class Sample
{
public string p1, p2;
public Sample()     // Default Constructor
{
p1 = "Welcome";
p2 = "Hello rajnish";
}
}
class Program
{
static void Main(string[] args)
{
Sample obj=new Sample();   // Once object of class created automatically constructor will be called
Console.WriteLine(obj.p1);
Console.WriteLine(obj.p2);
Console.ReadLine();
}
}
}

Output


Welcome
Hello rajnish

Parameterized Constructors

The constructor contain one parameter  in Constructor method call Parameterized Constructor




using System;
namespace ConsoleApplication3
{
class Sample
{
public string P1, P2;
public Sample(string x, string y)     // Declaring Parameterized constructor with Parameters
{
P1= x;
P2= y;
}
}
class Program
{
static void Main(string[] args)
{
Sample obj=new Sample("Welcome","Hello Rajnish");   // Parameterized Constructor Called
Console.WriteLine(obj.P1+" to "+ obj.P2);
Console.ReadLine();
}
}
}

Output 

Welcome to Hello rajnish

Constructor Overloading

Constructor with different  parameter but same name called the Constructor Overloading




using System;
namespace ConsoleApplication3
{
class Sample
{
public string P1, P2;

public Sample()     // Default Constructor
{
P1= "Hi";
P2 = "I am Default Constructor";
}
public Sample(string x, string y)     // Declaring Parameterized constructor with Parameters
{
param1 = x;
param2 = y;
}
}
class Program
{
static void Main(string[] args)
{
Sample obj = new Sample();   // Default Constructor will Called
Sample obj1=new Sample("Welcome","Hello Rajnish");   // Parameterized Constructor will Called
Console.WriteLine(obj.P1+ ", "+obj.P2);
Console.WriteLine(obj1.P1+" to " + obj1.P2);
Console.ReadLine();
}
}

Output

Hi, I am Default Constructor
Welcome to Hello Rajnish


Static Constructor

When we declared constructor as static it will be invoked only once for any number of instances of the class and it’s during the creation of first instance of the class or the first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.


using System;
namespace ConsoleApplication3
{
class Sample
{
public string P1, P2;
static Sample()
{
Console.WriteLine("Static Constructor");
}
public Sample()
{
P1= "Sample";
P2= "Instance Constructor";
}
}
class Program
{
static void Main(string[] args)
{
// Here Both Static and instance constructors are invoked for first instance
Sample obj=new Sample();
Console.WriteLine(obj.P1+ " " + obj.P2);
// Here only instance constructor will be invoked
Sample obj1 = new Sample();
Console.WriteLine(obj1.P1 +" " + obj1.P2);
Console.ReadLine();
}
}
}

When we run above program it will show output like as shown below
Output
Static Constructor
Sample Instance Constructor
Sample Instance Constructor

Notes

- it will not accept any parameters because it is automatically called by CLR.
- it will not have any access modifiers.
- it will execute automatically whenever we create first instance of class
- Only one static constructor will allowed.

Copy Constructor 

 A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.

using System;
namespace ConsoleApplication3
{
class Sample
{
public string P1, P2;
public Sample(string x, string y)
{
P1= x;
P2= y;
}
public Sample(Sample obj)     // Copy Constructor
{
P1= obj.P1;
P2= obj.P2;
}
}
class Program
{
static void Main(string[] args)
{
Sample obj = new Sample("Welcome", "Hello Rajnish");  // Create instance to class Sample
Sample obj1=new Sample(obj); // Here obj details will copied to obj1
Console.WriteLine(obj1.param1 +" to " + obj1.param2);
Console.ReadLine();
}
}
}

When we run above program it will show output like as shown below

Output 
Welcome to Hello Rajnish

Private Constructor

Private constructor is a special instance constructor used in a class that contains static member only. If a class has one or more private constructor and no public constructor then other classes is not allowed to create instance of this class this mean we can neither create the object of the class nor it can be inherit by other class. The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static.

using System;
namespace ConsoleApplication3
{
public class Sample
{
public string P1, P2;
public Sample(string a,string b)
{
P1= a;
P2= b;
}
private Sample()  // Private Constructor Declaration
{
Console.WriteLine("Private Constructor with no prameters");
}
}
class Program
{
static void Main(string[] args)
{
// Here we don't have chance to create instace for private constructor
Sample obj = new Sample("Welcome","to Hello Rajnish");
Console.WriteLine(obj.P1+" " + obj.P2);
Console.ReadLine();
}
}
}

Output
Welcome to Hello Rajnish

 In above method we can create object of class with parameters will work fine. If create object of class without parameters it will not allow us create.

// it will works fine
Sample obj = new Sample("Welcome","to Hello-Rajnish");
// it will not work because of inaccessability
Sample obj=new Sample();

Notes

 - One use of private construct is when we have only static member.
 - Once we provide a constructor that is either private or public or any, the compiler will not allow us      to add public constructor without parameters to the class.
 - If we want to create object of class even if we have private constructors then we need to have             public constructor along with private constructor 

Comments

Popular Posts