Tuesday, March 16, 2010

What is constructor?

A class can contain one or more methods to build and initialize objects. These methods are called constructors. Constructors are divided into three categories:
•    Instance constructors
•    Private constructors
•    Static constructors
Instance constructors: -
Instance constructors are the most common and sometimes referred to as just constructors. A constructor is named the same as the class. It is invoked when we create a new object. For example:
Point myPoint = new Point();
The class might also contain a constructor with parameters, such as:
Point myPoint = new Point(int x, int y);
If the class doesn’t contain a constructor, the default constructor is invoked. The default constructor is a parameter less constructor such as Point(). This constructor initialize the created object with default parameters – zero in case of integer fields.
Example: -
//Example 5-9.cs
//Construct example
using  System;
class Point
{
    public int x,y;
    //Default constructor:
    public Point()
    {
        x = 0;
        y = 0;
    }
    // A constructor with parameters
    public Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
}
class MyClass
{
    static void Main()
    {
        Point p1 = new Point();
        Point p2 = new Point(2,10);
        Console.WriteLine(“First Point at: ({0}, {1})”, p1.x, p1.y);
        Console.WriteLine(“Second Point at: ({0},{1})”, p2.x,p2.y);
    }
}

Declaring Constructors: -
Although the default parameter less constructor is automatically created by the compiler, we cannot declare one constructor with parameters without declaring the parameter less constructor. The compiler provides the default constructor only if the class does not have any constructors. In other words, in order to use the statement:
Point p1 = new Point();
We must declare a parameter less constructor.
Using this: -
In C# the this keyword is used to access data members in instance constructors (methods or accessors). For example, we can declare the constructor in the preceding example like this:
public Point(int x, int y)
{
    this.x = x;
    this.y = y;
}
In this case, we can use the same names for both fields and parameters (x and y). This is because the expression “this.x” represents the field “x” of the current object, while “x” is the method parameter. It is obvious that the keyword this cannot be used with static constructors or fields.
Private Constructors: -
Private constructors are used with classes that contain only static members. Other classes, except those nested in the same class, are not allowed to crate instances of that class.
Consider the following example:
public class MyClass
{
    private MyClass() { } // private constructor
    public string comapnyName;
    public string employmentDate;
}
In this example, the private constructor is an empty constructor whose job is to prevent the generation of a default constructor for the class. Notice that the private constructor uses the access modifier private, which is a common convention but not necessary because the default access level is private. It is recommended to declare the class as a static class when its members are all static.
Static Constructors: -
A static constructor is used to initialize a class. It is called before any objects are created and before any call to any static member of the class.
In the following example, the static constructor MyClass contains two printing statements. There is also a method, MyMethod, that contains another printing statement. Notice that when MyMethod is called, the static constructor is automatically invoked.
Example: -
//Example 5-10.cs
//Static constructor example
using System;
class MyClass
{
    //Static constructor:
    static MyClass()
    {
        Console.WriteLine(“Hey, I am the static constructor!” + “I am called automatically!”);
    }
    public void MyMethod()
    {
        Console.WriteLine(“Hi, I am MyMehod. I was called after “ + “the static constructor had been invoked!”);
    }
}
class MainClass
{
    static void Main()
    {
        MyClass myObject = new MyClass();
        myObject.MyMehod();
    }
}

No comments:

Post a Comment