Constructors in C#


Introduction

A constructor is a special method which runs automatically when we create an instance of a class. Please note the following points here which are applicable:

  1. Constructors have the same name as the class.
  2. Constructors can take parameters.
  3. Constructors never return a value; not even void.
  4. Every class must have a constructor, if we don't write a constructor then the compiler generates a default one which doesn't actually do anything.
  5. A default constructor has public accessibility.

//Example of a constructor
class ClassRoom
{
   
private int boycount;   //field

    public ClassRoom()     //default constructor
   
{
       
boycount = 30;
   
}

      public double Avg()     //method
   
{
       
//statements goes here
   
}
}

In the above example, the constructor is marked public. If we omit this keyword, the constructor will be private and it will have property like methods and fields. If we make the constructor private then obviously it cannot be used outside the class, which will prevent creation of ClassRoom objects from methods that are not part of the ClassRoom class. Sometimes we will get an exception here. We might therefore think that private constructors are not that valuable, however, they do have uses. Don't get confused here too, to check this, change any constructor to private and look at the result.

Now, to use the ClassRoom class and its Area method, dot notation will be used to invoke the Area method on a ClassRoom object:

ClassRoom r;    //creating a class variable, object

r = new ClassRoom();   //initializing class variable

double newAvg = r.Avg();   //invoking class method

In the above example, we have created a new class variable named r and in just the next line we are initializing all components of ClassRoom class to new r class variable. After this, we are invoking the class method by using dot(.) operator and assigning the value in newAvg.

Constructors Overloading

Untill now we discussed the basics of contractors. Let's have some discussion of overloading them. In the above example, ClassRoom objects will always be 30 because the default constructor sets the boycount to 30 and it stays at 30; the boycount field is private and there is no easy way of changing it's value after it has been initialized once. However, we should realize that a constructor is just a special kind of method and that's it. We knew that methods can be overloaded and constructors too. Constructor overloading is a type of Static Polymorphism. Using constructor overloading, we can define any number of constructors for the same class. But ensure that each constructor has a different number and type of parameters defined. Let's have some example:

//Example of constructor overloading
class ClassRoom
{
   
private int boycount;   //field

    public ClassRoom()     //default constructor
   
{
       
boycount = 30;
   
}

    public ClassRoom(int bcount)     //overloaded constructor
   
{
       
boycount = bcount;
   
}

     public double Avg()     //method
   
{
       
//statements goes here
   
}
}

In above example, we have created a new constructor having one parameter and we are assigning a parameter a value in field.

Now, to use the above new constructor we need to pass the parameter a value, look at the example:

ClassRoom r;

r = new ClassRoom(33);

Now the compiler will decide which constructor it should call based on the parameters that we have passed. In the above example we have passed an integer value that is 33. Please note the order of defined constructors can be anything, like in the above class example, we have two different constructors and anyone may be first.

HAVE A HAPPY CODING!! 


Recommended Ebook

Working with Directories in C#

Download Now!
Similar Articles