Thursday, April 11, 2013

Constructor Chaining in C#


What is a Constructor?

In OOP (that means Object-Oriented Programming);
  • A constructor in a class is a special type of method called to create an object
  • It prepares the new object for use and often accepting parameters that the constructor uses to set member variables required for the object to reach a valid state
  • Constructors often have the same name as the declaring class
Mainly two types;
  • Default constructors
-- Constructor that may be called without arguments
-- Maybe automatically generated in the absence of explicit constructors

ex.
        public House()
{ }
  • Parameterized constructors
-- Constructors that can take arguments
-- The number of arguments can be greater or equal to one

ex.
        public House(int howManyDoors, int howManyWindows, bool hasChimney)
            {
_howManyDoors = howManyDoors;
_howManyWindows = howManyWindows;
_hasChimney = hasChimney;
   }

Multiple constructors (Overriding constructors)

Most languages allow overloading the constructor in that there can be more than one constructor for a class, with differing parameters.

Example

class House{

private int _howManyDoors = 0;
private int _howManyWindows = 0;
private bool _hasChimney = false;

public House()
   {}

public House(int howManyDoors, int howManyWindows)
            {
_howManyDoors = howManyDoors;
_howManyWindows = howManyWindows;
   }

public House(int howManyDoors, int howManyWindows, bool hasChimney)
            {
_howManyDoors = howManyDoors;
_howManyWindows = howManyWindows;
_hasChimney = hasChimney;
   }
}

Chaining constructors

The problem of above Multiple constructors (Overriding constructors) concept is it duplicates code. (We are assigning a value to ‘_howManyDoors’ in all our constructors).

This is where constructor chaining is very useful. Constructor Chaining is an approach where a constructor calls another constructor in the same or base class. It will eliminate this problem. This time we only assign values in one constructor. And we call that constructor when the other two constructers are called. The constructor calling from this(...) is executed first.

Example

class House{

private int _howManyDoors = 0;
private int _howManyWindows = 0;
private bool _hasChimney = false;

public House() : this(0,0,false)
{
}

public House(int howManyDoors, int howManyWindows) : this(howManyDoors, howManyWindows, false)
        {

}

public House(int howManyDoors, int howManyWindows, bool hasChimney)
        {
_howManyDoors = howManyDoors;
_howManyWindows = howManyWindows;
_hasChimney = hasChimney;
}
}

Another example with some initialization in the default constructor and a parameterized constuctor

class Car{

private int _howManyDoors = 0;
private int _howManyWindows = 0;

public Car()
{
//Some init()
}

public Car(int howManyDoors, int howManyWindows) : this()
        {
_howManyDoors = howManyDoors;
_howManyWindows = howManyWindows;
}
}


References:

http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming)
http://msdn.microsoft.com/en-us/library/vstudio/ace5hbzh.aspx

http://www.codeproject.com/Articles/271582/Constructor-Chaining-in-Csharp

http://stackoverflow.com/questions/5555715/c-sharp-constructors-overloading
http://stackoverflow.com/questions/985280/can-i-call-a-overloaded-constructor-from-another-constructor-of-the-same-class-i?rq=1