Friday 27 May 2016

constructor

constructor

Constructor is used to initialize the member fields and property of class. It is same as the class name which does not allow any return type, not even void.
Constructor is useful when you need to implement or call very first time or on initialization. You can create different types of constructor within the class like private constructor, static constructor, default constructor, constructor chaining, constructor overloading for different types of parameter passed to an initialize on instance of class created.
You can also create destructor but it’s not required because .net framework CLR handle with garbage collector because it is very important to destroy or release resource of class object when it is not required to initialize.
List of constructor: I strongly recommended to understand this concept very clearly before start any application. It is very useful for your future projects.
  1. Default Constructor
  2. Constructor Overloading
  3. Private Constructor
  4. Constructor Chaining
  5. Static Constructor
  6. Destructors


A constructor without arguments or no parameters call default constructor.
There is no return type of constructor, not even void. The default constructor invoked the parameter-less constructor of the direct base class.

Defining default constructor:

using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClass cls = new myClass();
    }
}

class myClass
{
    public myClass()
    {
        Console.WriteLine("Instantiate...");

    }
} 
Output:
Instantiate…
Press any key to continue…
Constructor overloading must differ in their number of arguments or order of arguments.
Constructor overloading is the same as function overloading. Same name with different types of parameters.

How to define constructor overloading?


using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClass cls = new myClass(1,"Sample");

        Console.WriteLine("\n***********************************\n");

        //calling differnt parameter constructor
        myClass cls2 = new myClass(1, "Sample", "Constructor Overloading");
    }
}

class myClass
{
    private int Id;
    private string Name;

    public myClass()
    {
        Console.WriteLine("Instantiate...");

    }

    //constructor overloading with 2 parameters
    public myClass(int _id, string _name)
    {
        Id = _id;
        Name = _name;
        this.Print(null);
    }
    //constructor overloading with 3 parameters
    public myClass(int _id,string _name, string message)
    {
        Id = _id;
        Name = _name;
        this.Print(message);
    }

    //method to print member variable
    public void Print(string message)
    {
        Console.WriteLine("Id: {0}", Id);
        Console.WriteLine("Name: {0}", Name);

        if (message != null)
        {
            Console.WriteLine("Message: {0}",message);
        }
    }
}
Output:
Id: 1
Name : Sample
************************************
Id: 1
Name: Sample
Message: constructor overloading
Press any key to continue…
Yes, you can create private constructor using private access modifier.
Private constructor of the class and the public constructor of class can allow to access private constructor within the class through the constructor chaning.

How to define a private constructor?

using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClass cls = new myClass(1,"Sample");
    }
}

class myClass
{
    private int Id;
    private string Name;

    private myClass()
    {
        Console.WriteLine("Instantiate...\n");
    }

    //constructor overloading with 2 parameters
    public myClass(int _id, string _name):this()
    {
        Id = _id;
        Name = _name;
        this.Print();
    }
    //method to print member variable
    public void Print()
    {
        Console.WriteLine("Id: {0}", Id);
        Console.WriteLine("Name: {0}", Name);
    }
}
Output:
Instantiate…
Id: 1
Name: Sample
Press any key to continue…
What is the use of constructor chaining?
It is very useful technique for avoid code duplication. Using this() and base() you can access constructor and parameter fields of base class and derived class.
this() keyword indicate same class whereas base() indicated base class of derived class.

How to define constructor chaining?

using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClassT cls = new myClassT(1,"Sample");
    }
}

class myClass
{
    private int Id;
    private string Name;

    public myClass()
    {
        Console.WriteLine("Instantiate...\n");
    }

    //constructor overloading with 2 parameters
    public myClass(int _id, string _name):this()
    {
        Id = _id;
        Name = _name;
        this.Print();
    }
    //method to print member variable
    public void Print()
    {
        Console.WriteLine("Id: {0}", Id);
        Console.WriteLine("Name: {0}", Name);
    }
}

class myClassT: myClass
{
    public myClassT(int id, string name)
        : base(id,name)
    {
Console.WriteLine("Called constructor chaining through derived class...");
    }
}
Output:
Instantiate…
Id: 1
Name: Sample
Called constructor chaining through derived class...
Press any key to continue…
Static constructor is special type of constructor which does not have access modifier and also no parameter.
It is useful when you need to execute code on initialize the class before the first instance is created.
Static constructor is not call directly. User cannot control the execution of the static constructor.
Syntax:

static myClass()
    {
        Console.WriteLine("Static instantiate...\n");
    }
How to define static constructor?

using System;

class Program
{
   
    static void Main()
    {
        //instance of class is created here
        myClass cls = new myClass();
    }
}

class myClass
{
    static myClass()
    {
        Console.WriteLine("Static instantiate...\n");
    }
    public myClass()
    {
        Console.WriteLine("Default instantiate...\n");
    }
}
Output:
Static instantiate…
Default instantiate…
Press any key to continue…
  • Constructor is the same name as the class name.
  • Purpose of constructor is for initialize of member variable.
  • It is automatically invoked when object of class is created.
  • A constructor has not any return type, not even void.
  • You cannot call the constructor explicitly.
  • If you want to execute code on initialize you can put into constructor of class.
you can also visit:  www.mybook-lang.com

No comments:

Post a Comment