C# Method Overloading
Last modified: July 11, 2021Criteria for method overloading
- Same name but a different signature
- Different number of parameters or different type
- Can't overload by just changing the return type
As you can see we have two methods (one for integer and another for double) to display the addition of two values. However, it is not necessary to have two different names for methods.
static void AddInt(int x, int y)
{
Console.WriteLine("Add int");
}
static void AddDouble(double x, double y)
{
Console.WriteLine("Add double");
}
Instead we can use same method with different types of parameters. This is called Method Overloading
static void Add(int x, int y)
{
Console.WriteLine("Add int");
}
static void Add(double x, double y)
{
Console.WriteLine("Add double");
}
static void Add(double x, double y, double z)
{
Console.WriteLine("Add double");
}