C# Simplified, part 2: Methods & Programming Constructs - Overloading Methods
(Page 3 of 6 )
In listing 2.5, we declared a single method named display(). This method contains two integer parameters. You can also declare the same method once again in the same class, but it should contain different parameters. This process is known as Method Overloading.
If you attempt to declare the method with the same parameter, then the C# compiler won't know which one is the correct method, and there will be compilation errors. Listing 2.6 declares two methods named display(). The first method is declared with one integer parameter and the second one with two integer parameters. Inside the Main() method, show() method is called by passing the respective values.
Listing 2.6
using System;
class Methodover
{
//Method display() declared with one integer parameter
void display(int x)
{
Console.WriteLine(x);
}
//Method display() declared with two integer parameters
void display(int a, int b)
{
Console.WriteLine(a);
Console.WriteLine(b);
}
public static void Main()
{
//Object of the class created
Overloadmethod over = new Overloadmethod ();
//display() Method called by passing respective values
over.display(400);
over.display(600,800);
}
}
Next: Different types of programming constructs in C# >>
More C# Articles
More By Anand Narayanaswamy