Thursday, April 25, 2013

C#- Constructor Chaining in C#

Constructor Chaining
In this post, I am explaining about Constructor Chaining in  C#. I konw this will be taken for granted for those of us who have been working with  C# for a while now but may be a newbie will find this helpful.

In my previous posts, I explained Convert a Generic List to a Datatable, Get Property Names using Reflection in C#Hard drive information using C#Create Directory/Folder using C#Check Internet Connection using C#SQL Server Database BackUp using C# and some other articles related to C#ASP.Net jQuery, Java Script. and SQL Server.

What is Constructor Chaining? 

Constructor chaining is an approch where a constructor calls another contructor in the same class or base class.

Constructor chaining is very useful when we have a class that defines multiple constructors. Before knowing how to use constructor chaining in C#. Let's first see why we need constructor chaining.

Suppose we have a class call "Novel" and this  class consist of 4 constructor. Our class is look like as shown below-
class Novel
{
  private string _title;
  private string _author;
  private string _category;
  private string _publication;     
  public Novel(string Title)
  {
     this._title = Title;
  }
  public Novel(string Title,string Author)
  {
    this._title = Title;
    this._author = Author;
  }
  public Novel(string Title, string Author,string Category)
  {
    this._title = Title;
    this._author = Author;
    this._category = Category;
  }
  public Novel(string Title, string Author, string Category,string Publication)
  {
     this._title = Title;
     this._author = Author;
     this._category = Category;
     this._publication = Publication;
   }        
}

You can now ask here "what is wrong in this code?" and my answer is no there is no problem with the class shown above. But If you see the above class, you can see that there is an issue of duplicate code. Here we are duplicating the same code like "this._title = Title;" in all the constructor.

This is the case where constructor chaining is very useful. It will eliminate the duplicate code issue. Here is the modified class with constructor chaining.
class Novel
{
  private string _title;
  private string _author;
  private string _category;
  private string _publication;     
        
  public Novel(string Title)
  {
     this._title = Title;
  }
  public Novel(string Title,string Author) :this(Title)
  {
     this._author = Author;
  }
  public Novel(string Title, string Author,string Category) :this(Title,Author)
  {           
     this._category = Category;
  }
  public Novel(string Title, string Author, string Category, string Publication) : this(Title, Author,Category)
  {            
    this._publication = Publication;
  }        
}
In the above code you can see that each constructor is calling another constructor using "this" keyword. So we don't need to write code for assigning the values again and again. Here using "this" is similar to using ":base()".

I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.

No comments:

Post a Comment

^ Scroll to Top