Method Overriding and Optional Parameters

前端 未结 1 894
遥遥无期
遥遥无期 2020-12-29 20:33

Would someone care to explain how this code produces the folowing output?

using System;

namespace ConsoleApplication1
{
    class Test
    {
        public          


        
相关标签:
1条回答
  • 2020-12-29 20:51

    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.

    0 讨论(0)
提交回复
热议问题