Monday 25 February 2013

CONSTRUCTORS IN C# .NET


What is constructor?
  • It is a special type of method and name of the class and constructor is same
  • Constructor pass his all function to the new keyword because than new keyword will allocate memory for them
  • Constructor allow only the non static variable for memory allocation for them
  • It never be return type or also not void type
  • We cann’t used inheritance also
  • Constructor is used to initialize an object (instance) of a class.
  • Constructor is a like a method without any return type.

Constructors generally following types :
  • Default Constructor
  • Parameterized constructor
  • Copy Constructor
  • Static constructor
  • Private constructor
The constructor goes out of scope after initializing objects.

Default Constructor

A constructor that takes no parameters is called a default constructor.
When a class is initiated default constructor is called which provides default values to different data members of the class.

You need not to define default constructor it is implicitly defined.

Practical: Default Constructor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace default_constructor
{

class c1
{
int a, b;

public c1()
{
this.a = 10;
this.b = 20;
}

public void display()
{

            MessageBox.Show(a.ToString());
        MessageBox.Show(b.ToString());

  }

  }

  

  static void Main(string[] args)

  {

  // Here when you create instance of the class default constructor will be called.

  c1 ob1 = new c1();

  ob1.display();

  Console.ReadLine();

  }

  }

  }
//Write in Class Library 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace constrrec
{
    class employee
    {
        public employee()
        {
            MessageBox.Show("Hello manish.......!");
        }
    }
}
 
 
//Write in Window or console Application
 
 
        employee emp;
 
        private void button3_Click(object sender, EventArgs e)
        {
            emp = new employee();
 
            // that is Intilization of the emp , and when you Initilize the
            //class without parameter  then default constructor will be call
        }
 





Parameterized constructor


Constructor that accepts arguments is known as parameterized constructor. There may be situations, where it is necessary to initialize various data members of different objects with different values when they are created. Parameterized constructors help in doing that task.

Practical: Parameterized Constructor
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
    class Class1
    {

        int a,b;
       string h;
        public Class1()
        {
            this.a = 343;
            this.b = 234;
        }
        public Class1(int x, int z)
        {
            this.a = x + z;
            this.b = x - z;
        }
        public Class1(int s, int y, int r, int t)
        {
            s = y + r + t;
            this.a = s;
        }
        public Class1(string s, string b)
        {
            this.h = s + b;
        }
           
           
        public void display()
        {
            MessageBox.Show(a.ToString());
            MessageBox.Show(b.ToString());
            MessageBox.Show(h.ToString());
        }
 
    }
}
s}
}

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

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 ob = new Class1();
            ob.display();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Class1 ob = new Class1(2, 4,4,9);
            //ob.display();
            Class1 ob = new Class1("welcome", "ducat");
            ob.display();
        }
    }
}
 




Static Constructors


C# supports two types of constructor, a class constructor static constructor and an instance constructor (non-static constructor).

Point to be remembered while creating static constructor:
1. There can be only one static constructor in the class.
2. The static constructor should be without parameters.
3. It can only access the static members of the class.
4. There should be no access modifier in static constructor definition.



Static members are preloaded in the memory. While instance members are post loaded into memory.
Static methods can only use static data members.




Practical: Static Constructor

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class test
{
static string name;
static int age;

static test()
{
    System.Windows.Forms.MessageBox.Show("welcome to static constructor");
name = "John Sena";
age = 23;
}

public static void display()
{
    System.Windows.Forms.MessageBox.Show("using static constructor");
    System.Windows.Forms.MessageBox.Show(name.ToString());
    System.Windows.Forms.MessageBox.Show(age.ToString());

}

    }
}

namespace constructor
{
       


Copy Constructor

If you create a new object and want to copy the values from an existing object, you use copy constructor.
This constructor takes a single argument: a reference to the object to be copied.

Practical: Copy Constructor
using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Text;

  

  namespace copy_constructor

  {

  

  class Program

  {

  

  class c1

  {

  int a, b;

  

  public c1(int x, int y)

  {

  this.a = x;

  this.b = y;

  }

  

  
 
 


  // Copy construtor 

  public c1(c1 a)

  {

  this.a = a.a;

  this.b = a.b;

  }

  

  public void display()

  {

  int z = a + b;

  Console.WriteLine(z);

  }

  }

  

  


  static void Main(string[] args)

  {

  c1 ob1 = new c1(10, 20);

  ob1.display();

  // Here we are using copy constructor. Copy constructor is using the values already defined with ob1

  c1 ob2 = new c1(ob1);

  ob2.display();

  Console.ReadLine();

  }

  }

  }

Copy constructor sets behavior during runtime. It is shallow copying.
 
Private constructors
Example
Private constructors are used to restrict the instantiation of object using "new" operator.  A private constructor is a special instance constructor. It
is commonly used in classes that contain static members only.

This type of constructors is mainly used for creating singleton object.
If you do not want the class to be inherited we declare its constructor
private.
We can not initialize the class outside the class or the instance of class
can not be created outside if its constructor is declared private.
We have to take help of nested class (Inner Class) or static method to
initialize a class having private constructor.

Note:- Private constructor should be parameter less

//In class library 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace constrrec
{
    public class employee
    {  
       private employee()
        {
         }      
    } 
       
}
 
// when you will create the object of the employee class and compile the or execute the program  
// then you will find an error .
 
// LIKE
 
------------------------------------------------------------------------------
 
employee obj =new employee();



CONSTRUCTOR CHAINING

constructor chaining is the chain of the constructor which is executed sequencely. It depends of the overload of the constructor , actually it is 
part of the Inheritance This concept makes sure that the matching base class 
constructor must be called based on the parameter passed to the child class 
constructor.

EXAMPLE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace WindowsFormsApplication1
{
    class abc
    {
        public abc()
        {
            System.Windows.Forms.MessageBox.Show("welcome");
        }
        public abc(int a)
            : this()
        {
            System.Windows.Forms.MessageBox.Show("a=="+a);
        }
 
        public abc(int a,int b):this(a)
        {
            int c = a + b;
            System.Windows.Forms.MessageBox.Show("a+b=="+c);
 
        }
    }
}
 
//write in window application
 
 
      abc obj;      //class object
        private void button1_Click(object sender, EventArgs e)
        {
            obj = new abc(1,9);
        }
 
 // I  just  passed these value to call only one constructor but it will return three answer ...........
 // these are the following .






No comments:

Post a Comment