+ 1
Console.WriteLine("Hello World"); and Console.WriteLine("Hello"); is this a method overload?
Hey! Recently I thought that method overloading is the same method, but with different parameters, does it mean, for example, that Console.WriteLine ("Hello World"); and Console.WriteLine ("Hello"); is this a method overload? After all, this is the same method, but with different parameters.
5 Answers
+ 4
"Hello World" and "Hello" are a single string and only passed as single argument. Multiple arguments will be seperated using ", " (Or any other delimiter!)
+ 1
Method overloading is when you have more than one version of the same method, but with different parameters, either such difference being by number, type or order.
The return type doesn't determine nothing here.
The name of the method must be identical, and must be owned by the same class/inheritance hierarchy.
In your example you're passing a string, the only difference is the content of such string, so you're calling the same version of the method in both.
Your miss-understanding is to don't know the difference between argument and parameter.
* Parameter - is what the method expects on its definition.
* Argument - is what is passed to the method whet it is call from outside.
+ 1
Examples of overloading
-------
public int method(int x)
{
return x+4;
}
public int method()
{
return 7;
}
public int method(int x,int y)
{
x=7;
y=2;
return x+y;
}
0
// method one
public static int method(int x)
{
return x+4;
}
//method Two
public static int method()
{
return 5;
}
//method thire
public static int method(int x,int y)
{
return x+y;
}
static void Main(string[] args)
{
//Examples of overloading
Console.WriteLine("the method one ={0}",method (3));
Console.WriteLine("the method two ={0}",method ());
Console.WriteLine("the method Third ={0}",method (2+8));
}