By 22/07/2015 11 Comments

Constructor Overloading In C#

  • When we use constructor overloading in C#, the C# implements polymorphism which is one of the principal/feature of an object oriented programming.
  • Polymorphism is a greek word which means having several different forms. In object oriented the same methods, objects and variables can be defined with several different forms/shapes which can be unique from each other by behavior or functionality.
  • When there are more than one or multiple constructors with the same name is defined inside the same class but their parameter list is different from one another, these constructors are called overloaded and this situation is called constructors overloading.
  • In constructors overloading each constructor’s parameter list must be different from one another by their type, number or sequence of parameters.
  • Overload constructors allow us to create many different forms / shapes of a same type object. These objects will be same in type but totally different from one another’s.
  • Overload constructors make a class flexible for creating multiple objects.


using System;
namespace csharpBasic
    class MarkSheet

    /* Different types of Fields / instance variables 
       are declared with private acess.*/

        private double AspMarks;
        private double PhpMarks;
        private double JsMarks;
        string StudentName;

   /* A constructor MarkSheet() is declared with empty parameters means 
      it is a default constructor.*/
        public MarkSheet()
            this.AspMarks = 80;

   /* A constructor MarkSheet() is declared with a 
      double type parameter.*/
        public MarkSheet(double aspMarks)
            this.AspMarks = aspMarks;

   /* A constructor MarkSheet() is declared with 
      2 double type parameters.*/
        public MarkSheet(double aspMarks, double jsMarks)
            this.AspMarks = aspMarks;
            this.JsMarks = jsMarks;

   /* A constructor MarkSheet() is declared with 
      2 double and 1 string type parameters.*/
        public MarkSheet(double aspMarks, string studentName, double phpMarks)
            this.AspMarks = aspMarks;
            this.StudentName = studentName;
            this.PhpMarks = phpMarks;

   // Double return type method returns asp marks.
        public double GetAspMarks()
            return this.AspMarks;

   // Double return type method returns javascript marks.
        public double GetJsMaks()
            return this.JsMarks;

   // String return type method returns a student name.
        public string GetStudentName()
            return this.StudentName;

   // Double return type method returns PHP marks
        public double GetPhpMarks()
            return this.PhpMarks;

    class Program
   // Static main method void type declaration.
        static void Main(string[] args)

   /* Note the following statements which are creating different types or shapes of objects 
      from same class type (MarkSheet) by calling the different overloaded versions of same 
      constructor MarkSheet() with its same name. Each statement calls a particular constructor 
      by passing different type of arguments to it, 
      those arguments are different by its numbers and types. */

            MarkSheet markSheetOne = new MarkSheet();
            MarkSheet markSheetTwo = new MarkSheet(90);
            MarkSheet markSheetThree = new MarkSheet(88, 60);
            MarkSheet markSheetFour = new MarkSheet(70, "ABC", 55);

   /* Following statements call the different return type 
      methods with above object's reference variables.*/
            Console.WriteLine("In marksheet 1:");
            Console.WriteLine(" marks: {0}", markSheetOne.GetAspMarks());

   // Following statement creates a new line space.

            Console.WriteLine("In marksheet 2:");
            Console.WriteLine(" marks: {0}", markSheetTwo.GetAspMarks());


            Console.WriteLine("In marksheet 3:");
            Console.WriteLine(" marks: {0}", markSheetThree.GetAspMarks());
            Console.WriteLine("Js marks: {0}", markSheetThree.GetJsMaks());


            Console.WriteLine("In marksheet 4:");
            Console.WriteLine("Student name: {0}", markSheetFour.GetStudentName());
            Console.WriteLine(" marks: {0}", markSheetFour.GetAspMarks());
            Console.WriteLine("Php marks: {0}", markSheetFour.GetPhpMarks());
        The Output will be:
        In marksheet 1: marks: 80
        In marksheet 2: marks: 90
        In marksheet 3: marks: 88
        Js marks: 60 
        In marksheet 4:
        Student name: ABC marks: 70
        Php marks: 55

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 .