Method Overloading
In this chapter you will learn:
How to overload methods
In C#, two or more methods within the same class can share the same name, as long as their parameter declarations are different. These methods are said to be overloaded.
It is not sufficient for two methods to differ only in their return types. The methods must differ in the types or number of their parameters.
The following code defines a class, Math. Math has two methods with the same name. It is legal since the signatures are different.
using System;/*from j a v a 2 s. co m*/
class Math
{
public static int add(int i, int j)
{
return i + j;
}
public static float add(float f, float f2)
{
return f + f2;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Math.add(1, 2));
Console.WriteLine(Math.add(1.1F, 2.2F));
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Class