Constructors In C Sharp

Constructors :-
Constructor is  a special method, which initializes the class member with default values or the value that is passed by the constructors.
v Constructors must have the same name as class name.
v Constructors does not return any types, not even void.
v Constructor can’t be get inherited, although a derived class constructor can call the base class constructor using “base” keyword
v A class has atleast one constructor also known as default constructor.(a constructor without parameter)
v We have to explicitly define a default constructor in case of overloading constructors.
v  Constructor overloading means defining multiple constructors of a class with different sets of parameters.
v A constructor can be called another constructor using “this” keyword with in single class.

Types of Constructors

vInstance Constructors :- Instance constructors are specific to an instance of a type and are used to initialize both static and instance fields of the type. 
vPublic Constructors (parameterless or parameterized)
vPrivate Constructors (parameterless or parameterized)
vProtected Constructors(pameterless or parameterized)
vStatic Constructors :-  Static constructors are used to initialize static field of the type. We can initialize the non-static fields also but for this we require the object reference of that type.

Private Constructor (Singleton Pattern)
The Singleton pattern defines the class in which only a single instance can exist.
It is useful for exposing read-only data, and static methods that do not rely on instance data.
v Rather than creating an instance of the class each time, the application obtains a reference to an existing instance using a static method of the class that implements the Singleton pattern.
If this is the first call to the method that returns the instance, the Singleton creates an instance, populates it with any required data, and returns that instance.
Subsequent calls to the method simply return this instance. The instance lifetime is that of the application domain.
v It is not possible to inherit  a class that has only a private constructor.

-------------------------------------------------------------------------------------------------------
Example of Private Constructor (Singleton Pattern).



using System;
sealed class Singleton
{
private static Singleton instance = null;
private static bool flag = false;


public static Singleton CreateInstance()
{
if(!flag)
{
flag = true;
instance = new Singleton();
}
return instance;
}
private Singleton(){}
public void Display()
{
Console.WriteLine("I am instance member");
}
}


class PrivateConstructorTestOne
{
static void Main()
{
                //Singleton s_instance = new Singleton();   this generate an error due to private constructor.
Singleton s_instance = Singleton.CreateInstance();
s_instance.Display();
}
}


----------------------------------------------------------------------------------------------------------------------------




--------------------------------------------------------------------------------------------------------------------------------



Second Example of Private Constructor.


using System;
sealed class Singleton
{
private static Singleton instance = null;
private string name="";


public static Singleton CreateInstance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
private Singleton(){}
public void SetName(string name)
{
               // use this keyword when class field member name and parameter name is same.
this.name = name; 
}


public void DisplayName()
{
Console.WriteLine("Value at name is " + name);
}
}


class PrivateConstructorTestTwo
{
static void Main()
{
Singleton s1_instance = Singleton.CreateInstance();
s1_instance.SetName("Amit");
Console.WriteLine("Call DisplayName by first reference variable");
s1_instance.DisplayName();


Singleton s2_instance = Singleton.CreateInstance();
s2_instance.SetName("Gopal");
Console.WriteLine("Call DisplayName by second reference variable");
s2_instance.DisplayName();


Console.WriteLine("Call DisplayName again by first reference variable");
s1_instance.DisplayName();
}
}


--------------------------------------------------------------------------------------------------------------------------------
Constructors In C Sharp Constructors In C Sharp Reviewed by Baljeet Singh on 10:47 Rating: 5

No comments:

Powered by Blogger.