Method Overloading in C#
Introduction to Method Overloading
Method overloading is a feature in C# that allows a class to have more than one method with the same name, as long as their parameter lists are different. The main advantage of method overloading is that it increases the readability of the program. It is a way to achieve polymorphism in C#.
How Method Overloading Works
In method overloading, methods must differ in the type or number of their parameters. The return type of the method can be the same or different. The compiler uses the number of parameters and their types to determine which version of the overloaded method to call.
Basic Example of Method Overloading
Consider the following example where we have a class with overloaded methods:
using System;
class Program
{
static void Main()
{
OverloadExample example = new OverloadExample();
Console.WriteLine(example.Add(5, 10)); // Output: 15
Console.WriteLine(example.Add(5.5, 10.5)); // Output: 16
Console.WriteLine(example.Add("Hello", "World")); // Output: HelloWorld
}
}
class OverloadExample
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public string Add(string a, string b)
{
return a + b;
}
}
In this example, the Add
method is overloaded three times with different parameter types: int
, double
, and string
. The appropriate method is called based on the arguments passed.
Advantages of Method Overloading
Method overloading offers several advantages:
- Increases the readability of the code.
- Improves code reusability.
- Makes the program more intuitive by using the same method name for similar actions.
Method Overloading with Different Number of Parameters
Method overloading can also be achieved by changing the number of parameters. Consider the following example:
using System;
class Program
{
static void Main()
{
OverloadExample example = new OverloadExample();
Console.WriteLine(example.Multiply(5, 10)); // Output: 50
Console.WriteLine(example.Multiply(5, 10, 2)); // Output: 100
}
}
class OverloadExample
{
public int Multiply(int a, int b)
{
return a * b;
}
public int Multiply(int a, int b, int c)
{
return a * b * c;
}
}
In this example, the Multiply
method is overloaded with different numbers of parameters. The appropriate method is called based on the number of arguments passed.
Method Overloading with Different Order of Parameters
Method overloading can also be done by changing the order of parameters. Consider the following example:
using System;
class Program
{
static void Main()
{
OverloadExample example = new OverloadExample();
example.Display(10, "Ten"); // Output: Number: 10, Text: Ten
example.Display("Ten", 10); // Output: Text: Ten, Number: 10
}
}
class OverloadExample
{
public void Display(int number, string text)
{
Console.WriteLine("Number: " + number + ", Text: " + text);
}
public void Display(string text, int number)
{
Console.WriteLine("Text: " + text + ", Number: " + number);
}
}
In this example, the Display
method is overloaded with different order of parameters. The appropriate method is called based on the order of arguments passed.
Conclusion
Method overloading is a powerful feature in C# that allows you to define multiple methods with the same name but different parameters. It improves code readability and reusability. By understanding and utilizing method overloading, you can write more intuitive and maintainable code.