SHASHANGKA SHEKHAR'S BLOG

Hi, I am Shashangka Shekhar, Working with Microsoft Technologies. This is my personal blog where I share some of my experiences.

Constructor

Constructor is a special method of a class or struct.

When a class or struct is created, its constructor is called. Constructors have the same name as the class or struct, and they usually initialize the data members of the new object.

Types of Constructors

Basically constructors are 5 types those are

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

 

Class Constructors:

Constructors are responsible for object initialization and memory allocation of its class. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class.

Here you need to remember that a class can have any number of constructors and constructors don’t have any return type, not even void and within a class we can create only one static constructor.

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.

 

Default Constructor:

A constructor without having any parameters called default constructor. In this constructor every instance of the class will be initialized without any parameter values like as shown below

OutPut:

Shashangka Shekhar

Parameterized Constructor:

A constructor with at least one parameter is called as parameterized constructor. In parameterized constructor we can initialize each instance of the class to different values like as shown below

OutPut:

Shashangka Shekhar

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. Check below example for this

OutPut:

Original: Shashangka Shekhar

Copied: Shashangka Shekhar

Static Constructor

A static constructor is a member that implements the actions required to initialize a class. Static constructors are declared using static-constructor-declarations.

Static constructors are not inherited, and cannot be called directly.

The static constructor for a class executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

  1. An instance of the class is created.
  2. Any of the static members of the class are referenced.
  3. Static constructor will not accept any parameters because it is automatically called by CLR.
  4. Static constructor will not have any access modifiers.
  5. Static constructor will execute automatically whenever we create first instance of class
  6. Only one static constructor will allowed.

OutPut:

Result: 1

Private Constructor:

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:

OutPut:

New count: 101

Error if Instantiated: ‘SampleClass.SampleClass()’ is inaccessible due to its protection level  

Struct Constructors:

Struct constructors are similar to class constructors, except for the following differences:

  • Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values.
  • A struct cannot have an initializer in the form: base (argument-list).

Example

The following is an example of a constructor using two arguments for the Point struct.

OutPut:

Point:1[default initialization]: (0,0)

Point:2[explicit initialization]: (10,15)

2 Comments

Post a comment