Member-only story
Method Overloading in C#
When you define multiple methods in a class with the same name but with different parameter lists. This method parameter must differ in the numbers, types, and order of parameters. It is called method overloading. Method overloading is a form of polymorphism.
Syntax of method overloading:
The below is syntax of the use of method overloading in C#:
Example
public class FruitUtility
{
// Method with no parameters
public int AddFruitPrice()
{
return 0;
}
// Method with two integer parameters
public int AddFruitPrice(int price1, int price2)
{
return price1 + price2;
}
// Method with three double parameters
public double AddFruitPrice(double price1, double price2, double price3)
{
return price1 + price2 + price3;
}
// Method with a different parameter type
public string AddFruitPrice(string a, string b)
{
return a +" "+ b;
}
}
In this example, we have a FruitUtility class with multiple AddFruitPrice methods, each having a different parameter list. Here are some key points about method overloading:
1. Method Signature:
The method signature consists of the method name and the parameter list (including the parameter names and their data types). Method overloads must differ in…