Last Updated : 18 Jan, 2025
Method overloading is an important feature of Object-Oriented programming and refers to the ability to redefine a method in more than one form. A user can implement method overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters within the same class. It differs by numbers, types, and order of parameters. There are some important points mentioned below:
Why do we need Method Overloading?
If we need to do the same kind of operation in different ways i.e. for different inputs. Like the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for a single action.
Different Ways of Method OverloadingMethod overloading can be done by changing:
We can achieve method overloading by changing the number of parameters of a method.
Example:
C#
// Overloading by changing the Number
// of parameters
using System;
class Geeks
{
// 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;
}
public static void Main(String[] args)
{
Geeks ob = new Geeks();
int sum1 = ob.Add(1, 2);
Console.WriteLine("add() with two integers");
Console.WriteLine("sum: " + sum1);
Console.WriteLine("add() with three integers");
int sum2 = ob.Add(1, 2, 3);
Console.WriteLine("sum: " + sum2);
}
}
add() with two integers sum: 3 add() with three integers sum: 62. Changing the Data types of the parameters
We can achieve method overloading by changing the data type of the method's parameter.
Example:
C#
// Overloading by changing the Data types
// of the parameters
using System;
class Geeks
{
// adding three integer values.
public static int Add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
// adding three double values.
public static double Add(double a,
double b, double c)
{
double sum = a + b + c;
return sum;
}
public static void Main(String[] args)
{
Console.WriteLine("Add() with integer parameter");
int sum2 = Add(1,2,3);
Console.WriteLine("sum: " + sum2);
Console.WriteLine("Add() with double parameter");
double sum3 = Add(1.0, 2.0, 3.0);
Console.WriteLine("sum: " + sum3);
}
}
Add() with integer parameter sum: 6 Add() with double parameter sum: 63. Changing the Order of the parameters
If the method has the same name but a parameters order of parameters so in this way we can also achieve the method overloading.
Example:
C#
// Illustration of overloading by changing the
// Order of the parameters
using System;
class Geeks
{
public void Identity(String name, int id)
{
Console.WriteLine("Name1 : " + name + ", "
+ "Id1 : " + id);
}
public void Identity(int id, String name)
{
Console.WriteLine("Name2 : " + name + ", "
+ "Id2 : " + id);
}
public static void Main(String[] args)
{
Geeks obj = new Geeks();
// by changing the order
obj.Identity("Geek", 1);
obj.Identity(2, "Geek2");
}
}
Name1 : Geek, Id1 : 1 Name2 : Geek2, Id2 : 2
Example: In this example, we understand what happens when the method signature is the same and the return type is different
C#
// Program to show error when method signature
// is the same and the return type is different
using System;
class Geeks
{
// 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;
}
public static void Main(String[] args)
{
// Creating Object
Geeks ob = new Geeks();
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);
}
}
Output:
Explanation: The compiler will give an error as the return value alone is not sufficient for the compiler to figure out which function it has to call. Only if both methods have different parameter types (so, they have different signatures), then Method overloading is possible.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4