31

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

8
  • 3
    Which line do you not understand? (It would be tedious to go into detail about all of them.) Commented Dec 11, 2012 at 17:13
  • Basically how the compiler chooses between the different methods, since they have identical signatures. Commented Dec 11, 2012 at 17:15
  • 1
    They don't have identical signatures... and again, it would help if you'd say which lines you're stuck on. How much have you tried to understand how overload resolution works? By the way, there's no such thing as C# 4.5. Commented Dec 11, 2012 at 17:16
  • It is written in specification. Commented Dec 11, 2012 at 17:16
  • 4
    The part about Console.WriteLine(one) and Console.WriteLine(two) is fairly straightforward. Console.WriteLine() takes an object. Your Test class overrides ToString(), so you get a fairly obvious result. Test2 does NOT override ToString(), and since WriteLine is looking for an object, it does not find the new string ToString() function. Commented Dec 11, 2012 at 17:22

1 Answer 1

50

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Good stuff Jon (as usual). I was slowly getting round to working it all out. The main potential surprise points being that it uses object.ToString() and not the ToString on the object and the fact it prefers newly defined methods to inherited ones.
Good Guy Jon Skeet: says "It would be tedious to go into detail about all of them", goes into detail about all of them.
Yes, that is quite illuminating! Sorry for the trouble. I was getting hung up on the same things as Chris, above - the object.ToString explains the difference between the override in one and new in two, and the fact that the compiler prefers newly declared methods explains the one.ToString().
And then says - this is a quick version. <shakes head>
@ananthonline: I didn't take the time to include spec references :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.