Pages

Wednesday, 26 February 2014

Constructors in C# with Example, Types of Constructor in C# with Example

Constructors in C# with Example, Types of Constructor in C# with Example

Generally constructor name should be same as class name. If we want to create constructor in a class we need to create a constructor method name same as class name check below sample method for constructor

class SampleA
{
public SampleA()
{
Console.WriteLine("Sample A Test Method");
}
}

 Types of Constructors

Basically constructors are 5 types those are

      1.    Default Constructor
      2.    Parametrized Constructor
      3.    Copy Constructor
      4.    Static Constructor
      5.    Private Constructor

1.    Default Constructor

 using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
public Sample()     // Default Constructor
{
param1 = "Welcome";
param2 = "http://fantasyaspnet.blogspot.in/";
}
}
class Program
{
static void Main(string[] args)
{
Sample obj=new Sample();   // Once object of class created automatically constructor will be called
Console.WriteLine(obj.param1);
Console.WriteLine(obj.param2);
Console.ReadLine();
}
}
}

Output:


Welcome
http://fantasyaspnet.blogspot.in/

Parametrized Constructors

 using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
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("Welcome","http://fantasyaspnet.blogspot.in/");   // Parameterized Constructor Called
Console.WriteLine(obj.param1 +" to "+ obj.param2);
Console.ReadLine();
}
}
}

Output:

Welcome to http://fantasyaspnet.blogspot.in/

Constructor Overloading

 using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;

public Sample()     // Default Constructor
{
param1 = "Hi";
param2 = "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","http://fantasyaspnet.blogspot.in/");   // Parameterized Constructor will Called
Console.WriteLine(obj.param1 + ", "+obj.param2);
Console.WriteLine(obj1.param1 +" to " + obj1.param2);
Console.ReadLine();
}
}

Output:
Hi, I am Default Constructor
Welcome to http://fantasyaspnet.blogspot.in/

Copy Constructor
 using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
public Sample(string x, string y)
{
param1 = x;
param2 = y;
}
public Sample(Sample obj)     // Copy Constructor
{
param1 = obj.param1;
param2 = obj.param2;
}
}
class Program
{
static void Main(string[] args)
{
Sample obj = new Sample("Welcome", "http://fantasyaspnet.blogspot.in/");  // 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();
}
}
}

Output:
Welcome to http://fantasyaspnet.blogspot.in/

Static Constructor

 using System;
namespace ConsoleApplication3
{
class Sample
{
public string param1, param2;
static Sample()
{
Console.WriteLine("Static Constructor");
}
public Sample()
{
param1 = "Sample";
param2 = "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.param1 + " " + obj.param2);
// Here only instance constructor will be invoked
Sample obj1 = new Sample();
Console.WriteLine(obj1.param1 +" " + obj1.param2);
Console.ReadLine();
}
}
}

Output:

Static Constructor
Sample Instance Constructor
Sample Instance Constructor

Private Constructor
 using System;
namespace ConsoleApplication3
{
public class Sample
{
public string param1, param2;
public Sample(string a,string b)
{
param1 = a;
param2 = 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 http://fantasyaspnet.blogspot.in/");
Console.WriteLine(obj.param1 +" " + obj.param2);
Console.ReadLine();
}
}
}

Output

Welcome to http://fantasyaspnet.blogspot.in/

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 http://fantasyaspnet.blogspot.in/");
 // it will not work because of inaccessability
 Sample obj=new Sample();


No comments:

Post a Comment