articles

Home / DeveloperSection / Articles / Method Overloading in C#

Method Overloading in C#

Anonymous User3987 05-May-2016

Method overloading is a feature which allows a class to have two or more methods having same name but different signature means (multiple behavior but different signature). The process of creating more than one method in a class with same name or creating a method in derived class with same name as a method in base class is called as method overloading.

Most of the programming languages supports a technique called default/optional parameters. It allows to programmer to make one or several parameter optional, by giving them a default value. 

For example: Create a class ‘MethodOverloading'. Here notice one thing, method are same but signature are different.

Source Code:

MethodOverloading class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OopsConcepts
{
    classMethodOverLoading
    { 
        publicint Addition(int a, int b)
        {
            int x;
            return x = a + b;
        }
        publicint Addition(int a, int b, int c)
        {
            int y;
            return y = a + b + c;                        
        }
        publicfloat Addition(float a, float b)
        {
            float u;
            return u = a + b;
        }
        publicfloat Addition(float a, float b, float c)
        {
            float v;
            return v = a + b + c;
        }
    }
}

OverloadingTest class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace OopsConcepts
{
    classOverLoadingTest                                             
    {
        staticvoid Main(string[]args)
        { 
          // Method Overloading Test
            MethodOverLoading mol = newMethodOverLoading();
            Console.WriteLine("Addition of {0}", mol.Addition(25, 25));
            Console.WriteLine("Addition of {0}", mol.Addition(2.5f, 2.5f));
            Console.ReadLine();
     }
    }
}

Updated 27-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By