Method overloading is a common way to implement polymorphism. This is the ability to redefine functions in multiple formats. Users can implement function overloading by defining two or more functions within a class with the same name. C# can distinguish between methods with different method signatures. That is, within the same class, methods may have the same name but different parameter lists (that is, number of parameters, order of parameters, and data types of parameters).

  • Overloaded methods are differentiated based on the number and type of the parameters passed as arguments to the methods.
  • You cannot define more than one method with the same name, Order and the type of the arguments. It would be compiler error.
  • The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.

Why do we need Method Overloading?

When you need to perform the same type of operation in different ways, i.e.in response to different inputs. The examples described below perform addition operations on various inputs. It’s difficult to give a single action many different meaningful names.

Different ways of doing overloading methods- 
Method overloading can be done by changing:  

  1. The number of parameters in two methods.
  2. The data types of the parameters of methods.
  3. The Order of the parameters of methods.

By changing the Number of Parameters 

/*
 C# program to demonstrate the function
overloading by changing the Number
of parameters */
using System;
class CodeConfigs {
 
    // adding two integer values.
    public int Add(int a, int b)
    {
        int sum = a + b;
        return sum;
    }
 
    // adding three integer values.
    public int Add(int a, int b, int c)
    {
        int sum = a + b + c;
        return sum;
    }
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // Creating Object
        CodeConfigs ob = new CodeConfigs();
 
        int sum1 = ob.Add(1, 2);
        Console.WriteLine("sum of the two "
                          + "integer value : " + sum1);
 
        int sum2 = ob.Add(1, 2, 3);
        Console.WriteLine("sum of the three "
                          + "integer value : " + sum2);
    }
}

Output:

sum of the two integer value: 3
sum of the three integer value: 6

By changing the Data types of the parameters


/*
 C# program to demonstrate the function
 overloading by changing the Data types
 of the parameters
*/
using System;
class CodeConfigs {
 
    // adding three integer values.
    public int Add(int a, int b, int c)
    {
        int sum = a + b + c;
        return sum;
    }
 
    // adding three double values.
    public double Add(double a,
                      double b, double c)
    {
        double sum = a + b + c;
        return sum;
    }
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // Creating Object
        CodeConfigs ob = new CodeConfigs();
 
        int sum2 = ob.Add(1, 2, 3);
        Console.WriteLine("sum of the three "
                          + "integer value : " + sum2);
        double sum3 = ob.Add(1.0, 2.0, 3.0);
        Console.WriteLine("sum of the three "
                          + "double value : " + sum3);
    }
}

Output:

sum of the three-integer value: 6
sum of the three double value: 6

By changing the Order of the parameters

/*
 C# program to demonstrate the function
 overloading by changing the
 Order of the parameters
*/
using System;
class CodeConfigs{
 
    // Method
    public void Identity(String name, int id)
    {
 
        Console.WriteLine("Name1 : " + name + ", "
                        + "Id1 : " + id);
    }
     
    // Method
    public void Identity(int id, String name)
    {
 
        Console.WriteLine("Name2 : " + name + ", "
                        + "Id2 : " + id);
    }
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // Creating Object
        CodeConfigs obj = new CodeConfigs();
 
        obj.Identity("Aman", 1);
        obj.Identity(2, "Naman");
    }
}

Output:

Name1 : Aman, Id1 : 1
Name2 : Naman, Id2 : 2

What happens when method signature is same, and the return type is different?
The compiler throws an error because the return value alone is not enough to determine which function should be called. Method overloading is possible only if both methods have different types of parameters (that is, different signatures).

Example:

// C# program to show error when method signature
// is the same and the return type is different.
using System;
class CodeConfigs
{
 
    // adding two integer value.
    public int Add(int a, int b)
    {
 
        int sum = a + b;
        return sum;
    }
 
    // adding three integer value.
    public double Add(int a, int b)
    {
        double sum = a + b + 0.0;
        return sum;
    }
 
    // Main Method
    public static void Main(String[] args)
    {
 
        // Creating Object
        CodeConfigs ob = new CodeConfigs();
 
        int sum1 = ob.Add(1, 2);
        Console.WriteLine("sum of the two "
                          + "integer value :" + sum1);
 
        int sum2 = ob.Add(1, 2);
        Console.WriteLine("sum of the three "
                          + "integer value :" + sum2);
    }
}

I hope you enjoyed reading this post. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can