By 22/07/2015 9 Comments

Method Overloading In C#

  • When we use method 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 methods (having return type void or something else) with the same name is defined inside the same class but their parameter list is different from one another, these methods are called overloaded and this situation is called method overloading.
  • In method overloading each method’s parameter list must be different from one another by their type, number or sequence of parameters.

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // Following is a field declaration of double type.
        private double AspMarks;

        // Following is void type method DisplayAspMarks() with no parameter-list.
        public void DisplayAspMarks()
        {
            Console.WriteLine("Asp.net marks: {0}", this.AspMarks);
        }

        // Following is void type method DisplayAspMarks() with an int type parameter.
        public void DisplayAspMarks(int aspMarks)
        {
            this.AspMarks = aspMarks;
            Console.WriteLine("Asp.net marks: {0}", this.AspMarks);
        }

        // Following is void type method DisplayAspMarks() with a double type parameter.
        public void DisplayAspMarks(double aspMarks)
        {
            this.AspMarks = aspMarks;
            Console.WriteLine("Asp.net marks: {0}", this.AspMarks);
        }

        // Following is void type method DisplayAspMarks() with double and int type parameters.
        public void DisplayAspMarks(double aspMarks, int graceAspMarks)
        {
            this.AspMarks = aspMarks + graceAspMarks;
            Console.WriteLine("Asp.net grace marks: {0}", this.AspMarks);
        }

        // Following is void type method DisplayAspMarks() with int and double type parameters but with change sequence.
        public void DisplayAspMarks(int graceAspMarks, double aspMarks)
        {
            this.AspMarks = (graceAspMarks + aspMarks);
            this.AspMarks = aspMarks;
            Console.WriteLine("Asp.net grace marks: {0}", this.AspMarks);
        }

        // Following is double return type method DisplayAspMarks() with an int and int type parameters.
        public double DisplayAspMarks(int aspMarks, int graceAspMarks)
        {
            this.AspMarks = aspMarks + graceAspMarks;
            return this.AspMarks;
        }

        // Following is double return type method DisplayAspMarks() with a float type parameter.
        public double DisplayAspMarks(float aspMarks)
        {
            this.AspMarks = aspMarks;
            return this.AspMarks;
        }
    }
    
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            MarkSheet markSheet = new MarkSheet();

         /* Following statements that calls the different overloaded versions of same method DisplayAspMarks() 
            with its same name. Each statement calls a particular method by passing different type of arguments to it, 
            those arguments are different by sequence, types or its numbers. */

            markSheet.DisplayAspMarks();
            markSheet.DisplayAspMarks(80);
            markSheet.DisplayAspMarks(80.5);
            markSheet.DisplayAspMarks(80.5, 10);
            markSheet.DisplayAspMarks(10, 80.5);

            // Print return type methods.
            Console.WriteLine("Asp.net grace marks: {0}", markSheet.DisplayAspMarks(40, 40));
            Console.WriteLine("Asp.net marks: {0}", markSheet.DisplayAspMarks(40f));
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Asp.net marks: 0
        Asp.net marks: 80
        Asp.net marks: 80.5
        Asp.net grace marks: 90.5
        Asp.net grace marks: 80.5
        Asp.net grace marks: 80
        Asp.net marks: 40
        
       */
    }
}






← 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 .