![]() |
VOOZH | about |
Method Overloading in C# is the ability to define multiple methods with the same name but different parameter lists.
Method overloading can be done by changing:
We can achieve method overloading by changing the number of parameters of a method.
Example: Overloading by changing the Number of parameters
add() with two integers sum: 3 add() with three integers sum: 6
We can achieve method overloading by changing the data type of the method's parameter.
Example: Overloading by changing the Data types of the parameters
Add() with integer parameter sum: 6 Add() with double parameter sum: 6
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:
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
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.