Friday, February 12, 2016

What is constructor in C#?



Constructor is special kinds of method which is automatically call when we create the instance or object of the class. It is also responsible for the object initialization and memory allocation of class.
Constructor name and class name should be same.

Constructor don’t return any value

We cannot inherited to the constructor

Constructor always public

Example:
Class class1
{
Public class1()
}
Types of constructor
The types of Constructor are given below:
1. Default constructor
2. Parameterized constructor
3. Private constructor
4. Copy constructor
5. Static constructor



Default constructor – A constructor without any parameter is called default constructor , in default constructor every object of the class will be initialized without any value the example of the default constructor is given below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public class Class1
    {
        public string s,s1;
        public Class1()
        {
            s = "welcome";
            s1 = "Dot Net darpan";
        }
      
       
    }
}
.cs code - 
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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Class1 obj = new Class1();
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(obj.s);
            MessageBox.Show(obj.s1);
        }
    }
}


output - 





Parameterized constructor – The constructor which have at least one parameter is called parameterized constructor .The example of the parameterized is given below: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public class Class1
    {
        public int i, j;
        public Class1(int x, int y)
        {
            i = x;
            j = y;
          
        }
      
       
    }
}
.cs code  

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Class1 obj = new Class1(10,20);
        private void button1_Click(object sender, EventArgs e)
        {
            int a = obj.i;
            int b = obj.j;
            MessageBox.Show(a.ToString());
            MessageBox.Show(b.ToString());

        }
    }
}


output :
10
20



Copy constructor – The constructor which creates an object by copy variable from other that type of constructor is called copy constructor .the main purpose of the copy constructor is to initialize new object to the value of an existing object. The syntax of the copy constructor is given below:
Public class class1
{
int I,j;
public class1(class1 o)
{
I=o.i;
J=o.j;
}
}
Class1 obj1=new obj1(10,20)
Class1 obj2=new obj2(obj1)
}
Static constructor – A static constructor is invoked only once for all instance of the class and it is invoked when we create the first instance of the class .The static constructor is used to initialize static field of the class the write the code which is executed only once.
Key points of static constructor
A static constructor does not contain any access modifier and also parameter less.
Static constructor is always public.
It cannot be call directly.
The syntax of the static constructor is given below:
Public class class1
{
Static class1()
{
Messagebox.show(“Static constructor”);
}
}

Private constructor – Private constructor is used to initialized the static member of the class .when a constructor is private , it is not possible for other classes to derive from this class.The sysntax of the private constructor is given below :
Public class class1
{
Private class1()
}

 



No comments:

Post a Comment