Friday, 3 August 2012

Constructor in C# with Example



What   is   Constructor:
 A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. It is special type function having following characteristics. 

1.    Name  of constructor should be same name of  class
2.    It  is  used to initialized  Data Member of Class
3.    Constructor  have no return type
4.    Automatic calling at the time of object creation
5.    Constructor  always return an object or  a memory  block, or we can say that Constructor behaves likes factor to produce  object
     

Types of   Constructor:
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor

Default Constructor: 

A constructor without any parameters is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values and it is not possible to initialize each instance of the class to different values.

Parameterized Constructor:

 A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.

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.

Static Constructor: 

You can create a constructor as static and when a constructor is created as static, it will be invoked only once for any number of instances of the class and it is 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.

Private Constructor: 

You can also create a constructor as private. When a class contains at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated when it contains every member as static. 




First   we create  a class……………Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Construstor
{
    class Student
    {
        public int id;
        public string name;

        //Default Constructor

        public Student()
        {
            MessageBox.Show("Default Constructor is  called");

        }

        //Static Constructor

        static Student()
        {
            MessageBox.Show("Static  Constructor is  called");
        }

        //Parameterized Constructor

        public Student(int i, string fn)
        {
            id = i;
            name = fn;

          MessageBox.Show("Paramerizied Constructor:" + " " + id.ToString() + " " + name);
        }
       
        //private Student()
        // {

        // }


        //Copy Constructor

        public Student(Student st1)
        {
            id = st1.id;
            name = st1.name;

            MessageBox.Show(id.ToString() + " " + name);
        }


    }
}


After   we  take  a  windows form and  taking 5  button  for  each constructor ……..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Construstor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        //Invoking Default Constructor

        private void bDefault_Click(object sender, EventArgs e)
        {
            Student st = new Student();
        }

        //Invoking Parameterized Constructor

        private void bParameterized_Click(object sender, EventArgs e)
        {
            Student st = new Student(26, "Kush Tiwari");

        }

        private void bPrivate_Click(object sender, EventArgs e)
        {

        }

        private void bCopy_Click(object sender, EventArgs e)
        {
         
         
            Student st = new Student(11, "Udayan Sir");

            //Invoking copy constructor
            Student st1 = new Student(st);
        }

        private void bStatic_Click(object sender, EventArgs e)
        {
            //Static Constructor and instance constructor, both are invoked for first instance.
            Student st = new Student();

            //Only instance constructor is invoked.
            Student st1 = new Student();

        }

    }
}



    2 comments:

    1. why did we not use any return type with constructor like Datatype and void keyword.

      ReplyDelete
    2. Can we use constructor in Abstract Class of which we can not create object .

      ReplyDelete