Would someone care to explain how this code produces the folowing output?
using System;
namespace ConsoleApplication1
{
class Test
{
public override string ToString() { return "ToString override"; }
public string ToString(string optional = "")
{ return String.Format("ToString with optional parameter {0}", optional); }
}
class Test2
{
public new string ToString() { return "ToString new"; }
public string ToString(string optional = "")
{ return String.Format("ToString with optional parameter {0}", optional); }
}
class Program
{
static void Main(string[] args)
{
Test one = new Test();
Test2 two = new Test2();
Console.WriteLine(one);
Console.WriteLine(one.ToString());
Console.WriteLine(one.ToString("foo"));
Console.WriteLine("--");
Console.WriteLine(two);
Console.WriteLine(two.ToString());
Console.WriteLine(two.ToString("bar"));
Console.ReadKey();
}
}
}
ToString override
ToString with optional parameter
ToString with optional parameter foo
--
ConsoleApplication1.Test2
ToString new
ToString with optional parameter bar
Okay, as there's general interest, here's a quick version:
Console.WriteLine(one)
This will use the WriteLine(object)
overload, which will in turn execute the object.ToString()
virtual method, overridden in One
- hence the output of ToString override
Console.WriteLine(one.ToString())
This will look at One
and see which methods have newly declared methods - discounting overrides. There's exactly one such method which is applicable - the one with the optional parameter. So that gets executed, using the default value, leading to output of ToString with optional parameter
.
Console.WriteLine(one.ToString("foo"))
Same again, but this time the compiler doesn't need to use the default value, hence ToString with optional parameter foo
Console.WriteLine(two)
Again, this will call the virtual object.ToString()
method from WriteLine(object)
. The method hasn't been overridden, so the default implementation returning the name of the type is used, leading to output of ConsoleApplication1.Test2
.
Console.WriteLine(two.ToString())
The compiler looks at all the method declared in Two
which aren't overriding virtual methods. In this case, there are two such methods - the parameterless one and the one with the optional parameter. The parameterless one is included because it's new rather than overriding a base class method.
The parameterless method is deemed a "better" candidate because the compiler prefers to call a method which doesn't need any optional parameters filling in. Hence the output is ToString new
Console.WriteLine(two.ToString("bar"))
Again, the compiler looks at all the method declared in Two
which aren't overriding virtual methods. In this case, there are two such methods - but the parameterless one isn't applicable, leaving just the one with the optional parameter. The compiler doesn't need to use the default value of the optional parameter here, as it's got an argument anyway... so the output is ToString with optional parameter bar
For much more on this, read the C# language specification - or for a half-way house, see my article on overloading.