Method overloading in C#

Method overloading is one of the most important part of object oriented programming. So let`s start with method overloading.

What is method overloading?

Method overloading is comes under the polymorphism, when we have two or more than two function in a program with same name but they all works for different – different purpose independently is known as the concept of function overloading.

Functions with same name but with different arguments are known as overloaded functions.

Example.

using System;
namespace method_overloading
{
    class Program
    {
       public static int addition(int a, int b)
       {
         return a + b;
       }
       public static float addition(float a, float b)
       {
         return a + b;
       }
       public class TestMemberOverloading
       {
          public static void Main(string[] args)
          {
            Console.WriteLine(Program.addition (12,23));
            Console.WriteLine(Program.addition(7.5f,9.3f));
            Console.ReadKey();
          }
       }
    }
}

Output.

35
16.8

As you can see, in above example we have two function with same name addition so the first function is working to add integers and second number is used to add floating point values.

The only difference between these both function is that they have different – different arguments, these arguments tells the compiler that which function should be call.