March 26, 2013

Constructors


Constructors enable the programmer to set default values, limit instantiation.

A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.

If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values

Constructors are mainly used to initialize private fields of the class while creating an instance for the class.

When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null. 
Types of constructors:

Default Constructor

A constructor that takes no parameters is called a default constructor. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.
                If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields.

For example, an int is initialized to 0. For more information on default values

Private Constructors:

A private constructor is a special instance constructor. It is generally used in classes that contain static members only.

 If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. 

For example:

The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you do not use an access modifier with the constructor it will still be private by default. 

However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

When class contains a private constructor then we cannot create a instance of that class.


Instance Constructors:

Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class.

 To initialize a static class, or static variables in a non-static class, you must define a static constructor
 class CoOrds
{
    public int x, y;

    // constructor 
    public CoOrds()
    {
        x = 0;
        y = 0;
    }
}       


Static Constructor:

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. 

It is called automatically before the first instance is created or any static members are referenced.

Static constructors are invoked only once and that is before creation of first instance of 
the class.



class SimpleClass
{
    // Static variable that must be initialized at run time. 
    static readonly long baseline;
 
    // Static constructor is called at most one time, before any 
    // instance constructor is invoked or member is accessed. 
    static SimpleClass()
    {
        baseline = DateTime.Now.Ticks;
    }
}


Static constructors have the following properties:

C  A static constructor does not take access modifiers or have parameters.

C  A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

C  A static constructor cannot be called directly.

C  The user has no control on when the static constructor is executed in the program.



Copy Constructor:

Unlike some languages, C# does not provide a copy constructor. 

If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.

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.

2 comments:

  1. Very Nice ...
    But some time Private and static looks same in means of using static members only.can u elaborate more plz

    ReplyDelete