C#- Function or Method Overloading

Function or Method Overloading

Method overloading, also known as Function overloading. Method overloading allows a class to have multiple methods with the same name. But with different signature. C# functions can be overloaded based on the number and type(int, float etc) of parameters. Signature of the method does not include return type and params modifier. So, It is not possible to overload a function based on return type and params modifier.

Method overloading is one of the ways that C# implements polymorphism

Here is a simple example that illustrates method overloading:

using System;
namespace MethodOverloadingDemo
{
    class KeenMath
    {
        public int max(int a,int b)
        {
            Console.WriteLine("Two Integer Parameters");
            return a > b ? a : b;
        }
        public float max(float a, float b)
        {
            Console.WriteLine("Two Float Parameters");
            return a > b ? a : b;
        }
        public double max(double a, double b)
        {
            Console.WriteLine("Two Double Parameters");
            return a > b ? a : b;
        }
        public char max(char a, char b)
        {
            Console.WriteLine("Two Char Parameters");
            return a > b ? a : b;
        }
        public int max(int a, int b,int c)
        {
            Console.WriteLine("Three Integer Parameters");
            return a > b ? (a>c? a :c):(b>c? b :c);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            KeenMath ob = new KeenMath();
            Console.WriteLine(ob.max(3, 5)); 
            Console.WriteLine(ob.max(33.66, 35.67));
            Console.WriteLine(ob.max(64.67F, 24.77F));
            Console.WriteLine(ob.max('A', 'a'));
            Console.WriteLine(ob.max(10, 20, 4));
        }
    }
}

The output from this program is shown here:

Two Integer Parameters
5
Two Double Parameters
35.67
Two Float Parameters
64.67
Two Char Parameters
a
Three Integer Parameters
20

C# provides certain implicit (i.e., automatic) type conversions. These conversions also apply to parameters of overloaded methods. For example, consider the following:

using System;
namespace MethodOverloadingDemo
{
    class KeenMath
    {
        public int max(int a,int b)
        {
            Console.WriteLine("Two Integer Parameters");
            return a > b ? a : b;
        }
       
        public double max(double a, double b)
        {
            Console.WriteLine("Two Double Parameters");
            return a > b ? a : b;
        }
      
      
    }
    class Program
    {
        static void Main(string[] args)
        {
            KeenMath ob = new KeenMath();
            int i=3,j=5;
            double x=10.5,y=35.67;
            float a=64.67F,b=24.77F; 
            Console.WriteLine(ob.max(i, j)); //calls ob.max(int,int) 
            Console.WriteLine(ob.max(x, y)); //calls ob.max(double,double)
            Console.WriteLine(ob.max(a, b)); //calls ob.max(double,double) -- type conversion 
           
        }
    }
}

The output from this program is shown here:

Two Integer Parameters
5
Two Double Parameters
35.67
Two Double Parameters
64.6699981689453