Click here to Skip to main content
15,035,283 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 11 Oct 2013

Stats

48.2K views
9 bookmarked

Constructor .NET

Rate me:
Please Sign up or sign in to vote.
4.41/5 (14 votes)
11 Oct 2013CPOL3 min read
Types of constructors available in C#

Introduction

Constructors have a very special meaning to Compiler and CLR but sometimes its flow seems difficult for a developer. Today I will explain simple but important insights of Constructor.

So, What is a Constructor?

A Constructor is a special method in a class/struct with the same name as that of class/struct without any return type, used to initialize fields and members of a class/struct;

A constructor can only be called by:

  • Compiler using New keyword to initialize a class/struct.
  • Another constructor of same class/struct using :this() keyword.
  • Constructors of derived class using :base() keyword.

Types of Constructor in C#?

  • Default constructor
  • Parameterized constructor
  • Instance constructor
  • Static constructor
  • Private constructor

Default Constructor?

  1. A Constructor with no parameter is called Default Constructor.
  2. Default Constructor is called by compiler when no arguments are passed to New operator while creating an object of class or struct.
  3. If there is no constructor in a Non-Static class, a Public Default Constructor is provided by compiler so that a class can be instantiated.
  4. A Struct cannot have an explicit Default Constructor (We cannot define an explicit Default Constructor in a struct), but it is always provided by compiler to initialize each field of struct to its default value.

Parameterized Constructor?

  1. A constructor with parameters is called parameterized Constructor.
  2. A Class or Struct can have multiple parameterized constructors as long as they have different method signature. They follow the same concept of method overloading.
  3. Compiler provides Default Constructors only if there is no constructor (Default or Parameterized) defined in a class.
  4. Parameterized Constructors can exist even without the existence of Default Constructors.

Static Constructor?

  1. To initialize a Static Class or Static variables in Non-Static Class, Static Constructors are used
  2. Access Modifiers are not allowed on Static Constructors
  3. Static Constructors cannot be parameterized
  4. There can be only one Static Constructors per class
  5. Static Constructors cannot have an explicit 'this' or 'base' constructor call, i.e., Static Constructors cannot be called directly
  6. Static Constructors are called automatically before the first instance of a class is created or any static member is referenced
  7. Static Constructors are called only once in the lifetime of a class

Private Constructor?

  1. A constructor becomes a private constructor when we declare with private access specifier.
  2. Private Constructors can neither be instantiated nor inherited from other class.
  3. Object of class can only be created in the class itself.
  4. Microsoft recommends its uses to implement Singleton Pattern.

Constructor Chaining?

  1. A constructor can make a call to another constructor of the same class or of base class;
  2. Since one constructor can invoke another, this sometimes can cause execution of multiple constructors and is referred to as Constructor chaining;
  3. If class is not derived from any other class, below would be the chain:
    1. Static Constructor
    2. Instance Constructor
  4. If a class is derived from any other class, below would be the chain:
    1. Derived Static Constructor
    2. Base Static Constructor
    3. Base Instance Constructor
    4. Derived Instance Constructor

I hope I have covered all topics related to constructor. Please let me know if I have missed any or if you need more explanation or examples.

!! Happy programming !!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SonuKSingh
Software Developer
India India
Programming is my passion. It is always fun to learn new things.

Follow my blog
Follow on FB

Comments and Discussions

 
QuestionOne more type is copy constructor Pin
Member 1267878915-Mar-19 1:29
MemberMember 1267878915-Mar-19 1:29 
QuestionOne more type is copy constructor Pin
Member 1267878915-Mar-19 1:29
MemberMember 1267878915-Mar-19 1:29 
SuggestionNice article Pin
Rahul VB21-Oct-16 12:01
professionalRahul VB21-Oct-16 12:01 
Hi,

This is a nice article.
But you can re-edit this to include code samples and that too simpler ones.
Any fresher can read your article and understand easily.

For example when you say:
Quote:
Types of Constructor in C#?

Default constructor
Parameterized constructor
Instance constructor
Static constructor
Private constructor


Then for every type of constructor give good examples. This will help the reader to understand.

Any ways, nice and informative
GeneralMy vote of 1 Pin
Warrick Procter15-Oct-13 21:54
MemberWarrick Procter15-Oct-13 21:54 
GeneralRe: My vote of 1 Pin
SonuKSingh16-Oct-13 5:08
professionalSonuKSingh16-Oct-13 5:08 
GeneralMy vote of 2 Pin
Qwertie15-Oct-13 18:29
MemberQwertie15-Oct-13 18:29 
GeneralRe: My vote of 2 Pin
SonuKSingh16-Oct-13 5:07
professionalSonuKSingh16-Oct-13 5:07 
GeneralMy vote of 3 Pin
dxs75314-Oct-13 1:59
Memberdxs75314-Oct-13 1:59 
GeneralRe: My vote of 3 Pin
SonuKSingh14-Oct-13 8:16
professionalSonuKSingh14-Oct-13 8:16 
GeneralMy vote of 2 Pin
dchrno12-Oct-13 5:23
Memberdchrno12-Oct-13 5:23 
GeneralRe: My vote of 2 Pin
SonuKSingh14-Oct-13 8:10
professionalSonuKSingh14-Oct-13 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.