By 22/07/2015 17 Comments

Call Overloaded Constructors Using this In C#

  • By using of the this keyword/reference, one constructor can callĀ to another, this is very useful when we work with overloaded constructors.
  • The main advantage of callĀ overloaded constructors through this keyword is that it prevents the unneeded duplicate code and are very useful.

General Syntax:

access-modifier ConstructorName (parameterList-1): this (parameterList-2)
{
// body of constructor.
}

STATEMENT DESCRIPTION
access-modifier It must be keyword public.
ConstructorName (parameterList-1) Original constructor name (class name) with optional parameter list.
: this (parameterList-2) The this keyword specifies which constructor should be called or invoke, it depends upon its parameter list which must be matched with other constructor (to be called). The this keyword automatically invoke/access other constructor when an original constructor will be called by new operator at the time of an object creation.
{ // body of constructor } Any logic or code can be placed here.

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {

   // Fields are declared with public access modifier.
        public double AspMarks;
        public double Html5;
        public double PhpMarks;
        public double JsMarks;
        public string StudentName;

   /* Following different versions of overloaded constructors MarkSheet() 
      are declared with different numbers of parameters. 
      Note when any particular constructor is called,
      first it calls the constructor which is defined by 
      the [this] keyword.*/
        public MarkSheet(double aspMarks)
            : this(50, 60)
        {
            this.AspMarks = aspMarks;
        }

        public MarkSheet(double phpMarks, double html5)
            : this("ABC", 90)
        {
            this.PhpMarks = phpMarks;
            this.Html5 = html5;
        }

        public MarkSheet(string studentName, double jsMarks)
        {
            this.StudentName = studentName;
            this.JsMarks = jsMarks;
        }
    }
    
    class Program
    {
   // Static main method void type declaration.
        static void Main(string[] args)
        {

   // Following statements create 2 mark sheet objects, each is unique from other.
            MarkSheet markSheetOne = new MarkSheet(80);
            MarkSheet markSheetTwo = new MarkSheet(markSheetOne.AspMarks, markSheetOne.Html5);

   // Following statements print each mark sheet information.
            Console.WriteLine("In marksheet 1:");
            Console.WriteLine("Asp.net marks {0}", markSheetOne.AspMarks);

   // Following statement creates a new line.
            Console.WriteLine();

            Console.WriteLine("In marksheet 2:");
            Console.WriteLine("Php marks: {0}", markSheetOne.PhpMarks);
            Console.WriteLine("Html5 marks: {0}", markSheetOne.Html5);


            Console.WriteLine();

            Console.WriteLine("In marksheet 3:");
            Console.WriteLine("Student name: {0}", markSheetTwo.StudentName);
            Console.WriteLine("Javascript marks: {0}", markSheetTwo.JsMarks);
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        In marksheet 1:
        Asp.net marks: 80
         
        In marksheet 2:
        Php marks: 50
        Html5 marks: 60
        
        In marksheet 3:
        Student name: ABC
        Javascript marks: 90 
         
       */
    }
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .