C# Method Overloading with examples

In the previous tutorial, we discussed the C# function with a different name. In this tutorial, you will learn about method overloading in C# programming. Two or more methods having the same name but different argument(s) is known as a method overload or method overloading in C#.

The function is a block of code that executes the statements for a specific purpose. We create a function for the different task with a different name but in method overloading, we can create function with the same name but different arguments. In C#, the functions have the same name if the number and/or type of arguments passed is different.

 

Let's understand the C# method overload with example.

C# method overloading example -


int Mul() { }// function for multiplication
int Mul(int x) { }
float Mul(double x) { }
int Mul(int x, double y) { }


In the above example, there are four functions having the same name but different argument(S).In C# method overloading, the data types may the same or different but argument(s) should be different.


Let's implement the example for method overloading in C#.

C# Method overloading example-


In this example, we will find out the multiplication of two and three numbers using different parameters.


using System;  
public class Calculator{ 
     
    public static int Mul(int x,int y){  
       return x*y;  
       
      
    }  
    public static double Mul(double x, double y, double w)  
    {  
         return x*y*w;  
      
    }  
}  
public class MethodOverloading  
{  
    public static void Main()  
    {  
        Console.WriteLine("Multiplication of x and y = "+Calculator.Mul(4, 8));  
        Console.WriteLine("Multiplication of x,y and w = "+Calculator.Mul(4.5, 3.5, 4.8));  
      
    }  
}

 

Output - 

Multiplication of x and y = 32
Multiplication of x,y and w = 75.6

In the example, we used int and float data types with two functions having the same name "Mul". In the above C# example, we used different data types with different number of the argument.

Let's create another example with the same data type.

C# method overloading with same data types -


using System;  
public class Calculator{ 
     
    public static int Mul(int x,int y){  
       return x*y;  
       
      
    }  
    public static double Mul(int x, int y, int w)  
    {  
         return x*y*w;  
      
    }  
}  
public class MethodOverloading  
{  
    public static void Main()  
    {  
        Console.WriteLine("Multiplication of x and y = "+Calculator.Mul(4, 8));  
        Console.WriteLine("Multiplication of x,y and w = "+Calculator.Mul(4, 5, 4));  
      
    }  
}

Output - 

Multiplication of x and y = 32
Multiplication of x,y and w = 80

In the above C# method overloading, we used the same data types but the different number of the argument.

C# method overloading: same data type and same number of the argument but different variables-


using System;  
public class Calculator{ 
     
    public static int Mul(int x,int y){  
       return x*y;  
       
      
    }  
    public static int Mul(int a, int b)  
    {  
         return a*b;  
      
    }  
}  
public class MethodOverloading  
{  
    public static void Main()  
    {  
        Console.WriteLine("Multiplication of x and y = "+Calculator.Mul(4, 8));  
        Console.WriteLine("Multiplication of a and b= "+Calculator.Mul(4, 5));  
      
    }  
}

Execute the above C# method overloading program and it gives the error.

main.cs(9,23): error CS0111: A member `Calculator.Mul(int, int)' is already defined. Rename this member or use different parameter types
main.cs(4,23): (Location of the symbol related to previous error) 

Why error?
The same number of arguments with the same data type.

Remove this error -

To remove this error use the different number of argument or use different data types.


C# method overloading: Same number of the argument but different data types-


using System;  
public class Calculator{ 
     
    public static int Mul(int x,int y){  
       return x*y;  
       
      
    }  
    public static float Mul(float a, float b)  
    {  
         return a*b;  
      
    }  
}  
public class MethodOverloading  
{  
    public static void Main()  
    {  
        Console.WriteLine("Multiplication of x and y = "+Calculator.Mul(4, 8));  
        Console.WriteLine("Multiplication of a and b= "+Calculator.Mul(4.6f,9.3f));  
      
    }  
}

Output -

Multiplication of x and y = 32
Multiplication of a and b= 42.78