SIGN UP MEMBER LOGIN:    
ARTICLE

Constructors in C#

Posted by Abhimanyu Kumar Vatsa Articles | C# Language June 20, 2011
In this article we will take a look at C# Constructors.
Reader Level:

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!! 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor