Wednesday, 27 November 2013

An Intro to Constructors in C#

 

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

A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.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.

 

Constructors can be classified into 5 types

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

 

 

To create a constructor, create a method in the class with same name as class and has the following syntax.

[Access Modifier] ClassName([Parameters])

{

}

 

image

 

 

 

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.

image

 

 

 

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.

 

image

 

image

 

 

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.

image

 

image

 

image

 

 

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.

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:

image

 

image

 

image

 

 

 

image

 

image

 

 

Some unique points related to constructors are as follows

  • A class can have any number of constructors.
  • A constructor doesn’t have any return type even void.
  • A static constructor can not be a parameterized constructor.
  • Within a class you can create only one static constructor.

 

 

 

Blogger Labels: Intro,Constructors,method,instance,zero,Default,Constructor,Copy,Static,Private,syntax,Modifier,ClassName,Parameters,parameter,Advantage,Main,purpose,instances,creation,reference,member,example,Some,Within

No comments:

Post a Comment