How to Use Constructors in C#

About the Article -> This article gives abrief introduction, features of constructors, their usage and also anidea of how to use a constructor in an object-oriented paradigm.

Level -> Beginner, Intermediate

Why Constructors?
Initialization of valuesof different variables is critical in any application development. C#does initialize simple data-types to default values as per the languagespecification in the .Net framework, but for classes that are userspecific, it is obvious that you would be the only one who can decidewhat is an ideal default value. The means of achieving this isConstructors. There is also a perception that constructors can only beused beyond initialization, which is true to some extent. We willexamine them in the following sections.

Features of a Constructor
* A Method that does not have a return type.
E.g.


public class in4DataGrid
{
//Constructor declaration. Note after the modifier "public" keyword, there is no
//mention of a datatype or void. Also the name is same as that of class.
public in4DataGrid()
{
// Some Code Here.
}
}

* Can either be static or Instance based.E.g.


public class in4DataGrid
{
//Static Constructor. Modifiers are irrelevant here.
static in4DataGrid()
{
// Some Code Here.
}
//Instance based
public in4DataGrid()
{
// Some Code Here.
}
}

* Take parameters and behave just like any other method, which means that any instructions can be encapsulated in a constructor.E.g.

public class in4DataGrid
{
private int intWidth;
private int intHeight;
//Parameters intWidth and intHeight of type int.
public in4DataGrid(int Width, int Height)
{
intWidth = Width;
intHeight = Height;
//Some Code Here.
}
}

* Constructors can be declared "private orprotected" even though declaring them public is a common scenario. Whendeclared as private, one cannot create an instance of the class orderive the class.
* Can be overloaded. Further, one overloaded constructor can call theother one to take advantage of encapsulation and take negate the risksof code duplication within overloaded constructors.(See the followingsection examples)

The following section deals with the how best to leverage the features of Constructors in our classes.

How to Use?

* Use application logic in constructorscarefully. Try to minimize unnecessary database operations inconstructors like loading a Dataset that is used only in specificsections or on demand from the calling class. This is a performanceoverhead.
* Do not write redundant code in overloaded constructors. This willbecome a huge maintenance issue as your class hierarchy grows and youstart implementing more overloaded constructors in your derivedclasses. The following example shows a common section where invalidation of initialization logic happens.
E.g.

public class in4DataGrid
{
private int intWidth;
private int intHeight;
//Overloaded constructor 1 with no parameters. But the constructor initializes
//the values by calling the other constructor which holds common business
//validations by using the keyword "this".
public in4DataGrid():this(200,300)
{
//Some Code Here.
}

//Overloaded constructor 2 with parameters intWidth and intHeight of type
//int.
public in4DataGrid(int Width, int Height)
{
intWidth = Width;
intHeight = Height;

// Do some business validation here.
if(intWidth > intHeight)
{
//Some code here
}
}
}

* Try to use static constructors and variableswhen you are dealing with Global variables. This is especially a gooddesign practice since you can initialize the static variables easilywithin static constructors instead of using constant variables. Otheradvantage would be to encapsulate your instruction logic for thesevariables.
E.g.


public class in4DataGrid
{
private int intWidth;
private int intHeight;
private static int intMinimumHeight;

//Overloaded static constructor 1 with no parameters. But the constructor
//initializes the values by calling the other constructor.
static in4DataGrid()
{
//Some Business logic Here.
//Initialize the value based on logic.
intMinimumHeight=100;
}

//Overloaded instance based constructor 2 with parameters intWidth and
//intHeight of type int.
public in4DataGrid(int Width, int Height)
{
if(Width > intMinimumWidth)
{
intWidth = Width;
intHeight = Height;
}
else
{
//Some Code Here.
}
}
}

* Overloaded static constructors are dangerouswhen used to call other constructors, so try not to call other staticconstructor from within.
* Design your derived class constructors carefully. This isbecause when an instance of derived class is created there is more thanone constructor that will be called, i.e. both the base class and thederived class in the hierarchy. The constructors of the derived classfirst will run constructors of base class. The issue arises when youhave overloaded constructor definitions in the base and derivedclasses. Overlooking here would prove costly. Make sure you use thekeyword "base()" to make calls to overloaded base classes.E.g.

public class in4DataGrid: System.Data.DataGrid
{
//Constructor declaration. Use of keyword "base()" makes sure that the call
//is made to the base class(here it is System.Data.DataGrid). One can call
//any overloaded constructor of the base class this way.
public in4DataGrid(): base()
{
// Some Code Here.
}
}

Also note that the constructor initializers"base" and "this" are the only keywords that can be used when making acall to the base constructor, also any one among these can bespecified.

Conclusion
Proper design of constructorsgoes a long way in solving some core problems faced in your classdesign. M.Net supports enormous flexibility to accommodate the use ofvarious types of constructors. One has to judge the need of theapplication and never overlook the importance of constructors.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!