Constructors in C#

Sanskruti's Avatar author of Constructors in C#
This is an article on Constructors in C# in C#.
Rated 5.00 By 2 users
If you go through any standard C# books you will see the constructors and so I would not get into the details of the constructors but what is more important to me is the use of static constructors which is somethine new in C# or you can say that its a copy of initiliazation block in Java.

Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization. If a class is not defined with the constructor then the CLR (Common Language Runtime) will provide an implicit constructor which is called as Default Constructor. A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.
  • Constructors do not return a value.
  • Constructors can be overloaded.
  • If a class is defined with static and Non-static constructors then the privilege will be given to the Non-static constructors.
The following are the access modifiers for constructors,
  • Public : A constructor that is defined as public will be called whenever a class is instantiated.
  • Protected : A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created.
  • Private : A constructor is defined as private in such cases whenever a class which contains only static members has to be accessed will avoid the creation of the object for the class.
  • Internal : An internal constructor can be used to limit concrete implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly.
  • External : When a constructor is declared using an extern modifier, the constructor is said to be an external constructor.

Types of Constructors

  1. Static : Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
    Code: CSharp
    Static ClassName()
    {
        //Initialization statements;
    }
  2. Non-Static : are used for initializing the Non-Static and Static members of a class. These will be invoked everytime a new object is defined for a class.
    Code: CSharp
    Public ClassName([argsInfo])
    {
        //Initialization statements;
    }
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I would add to the following types of constructors as follows
  • Public : A constructor that is defined as public will be called whenever a class is instantiated. Also the instantiation of the class can be from anywhere and from any assemblies.
  • Protected : A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created. In this case class object can only be created in the derived classes.
  • Private : A constructor is defined as private in such cases whenever a class which contains only static members has to be accessed will avoid the creation of the object for the class. The object of the class cannot be created and is used when any single instance of the class is needed and we have a static property or method to get the instance of the class.

Constructor Overloading



C# supports overloading of constructors, which means, we can have constructors with different sets of parameter signature. As an example :
Code: CSharp
public class MyClass
{
    public MyClass()
    {
        // This is the default constructor method.
    }

    public MyClass(int no)
    {
        // This is the constructor with one parameter.
    }

    public MyClass(int no, string Name)
    {
        // This is the constructor with two parameters.
    }
}

Calling other constructors



You can call the other constructor like this :
Code: CSharp
public class MyClass
{
    public MyClass(): this(10)  // We are calling the one parameter constructor.
    {
        // This is the default constructor method.
    }

    public mySampleClass(int Age)
    {
        // This is the constructor with one parameter.
    }
}
Now instead of calling the same class constructor you can also call the base class constructor using the base keyword. Initialization List is not possible to have any variable name in C# as it was the case with C++ and we can only have this or base.
rekha11's Avatar, Join Date: Jan 2008
Go4Expert Member
Constructor are same as the class name.Constructors will be automatically invoked whenever the object is created.Constructor have no return value .
constuctors are:
overloaded constructors
static constructors
copy constructor
alcheringa's Avatar, Join Date: May 2008
Newbie Member
Quote:
Originally Posted by rekha11
Constructor are same as the class name.Constructors will be automatically invoked whenever the object is created.Constructor have no return value .
constuctors are:
overloaded constructors
static constructors
copy constructor
copy constructors are needed to be defined explicitly. and it is not a matter of preference for non static versus static when a class is intialised. a class may be say both static and insitialised methods, it's during the usage of the static methods, the class calls static constructor.

even if a class has a static constructor and there's no constructor defined and we intiliased with the "new" keyword, static constructor would never be called.
juliaandrews's Avatar, Join Date: May 2011
Go4Expert Member
Hi,
In c# 5 access modifiers for constructors:-
Public
Protected
Private
Internal
External
Thanks
donor's Avatar
Banned
Code:
include <iostream>
using namespace std;

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}

int main () {
  CRectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}
Is there any error in program???

Last edited by shabbir; 12Jan2012 at 18:09.. Reason: Code blocks