重写方法中的默认参数值

时间:2021-03-15 17:07:45

In the following code, call to Method2 receives the Value parameter as False, even though base class does not declare default value for the parameter at all, and derived class declares True as default.
It could be argued (as was done in similar example here: C# optional parameters on overridden methods) that the compiler uses base class's method declaration first (which is true, as this behavior can be changed by prefixing call to Method1 with this.), but in this case, the base does not declare default value at all.
Is there a rational explanation for this?

在下面的代码中,对Method2的调用接收Value参数为False,即使基类根本没有声明参数的默认值,派生类声明True为默认值。可以认为(如在此类似示例中所做的那样:重写方法上的C#可选参数)编译器首先使用基类的方法声明(这是正确的,因为可以通过使用此前缀调用Method1来更改此行为。),但在这种情况下,基数根本不会声明默认值。对此有合理的解释吗?

using System;

class Base
{
    public virtual bool Method1(bool Value) { return true; }
    public virtual bool Method2(bool Value) { return true; }
}

class Derived : Base
{
    public override bool Method1(bool Value = true)
    {
        return Value;
    }

    public override bool Method2(bool Value = true)
    {
        return Method1();
    }
}

class Program
{
    static void Main(string[] args)
    {
        Derived a = new Derived();
        Console.WriteLine("Call to Method1, expected: True, got: {0}", a.Method1());
        Console.WriteLine("Call to Method2, expected: True, got: {0}", a.Method2());
    }
}

Output:

Call to Method1, expected: True, got: True
Call to Method2, expected: True, got: False

3 个解决方案

#1


1  

Looks like this is a type of bug.

看起来这是一种bug。

Here is the link you guys were talking about, I think its from earlier this year:

以下是你们正在谈论的链接,我认为它来自今年早些时候:

C# optional parameters on overridden methods

重写方法的C#可选参数

#2


1  

It seem that your question is related to this other question, which may help you: Ambiguity with inheriting an optional-parameter base method

看来你的问题与另一个问题有关,这可能会对你有所帮助:继承可选参数基本方法的歧义

#3


1  

I just installed Visual Studio 2012 RTM and the same code is working as expected even when compiled for Framework 3.5 or 2.0. So apparently this is a compiler issue, rather than .Net Framework one and has been fixed in the new version of the C# compiler.

我刚刚安装了Visual Studio 2012 RTM,即使在为Framework 3.5或2.0编译时,相同的代码也按预期工作。显然这是一个编译器问题,而不是.Net Framework 1,并且已在新版本的C#编译器中得到修复。

#1


1  

Looks like this is a type of bug.

看起来这是一种bug。

Here is the link you guys were talking about, I think its from earlier this year:

以下是你们正在谈论的链接,我认为它来自今年早些时候:

C# optional parameters on overridden methods

重写方法的C#可选参数

#2


1  

It seem that your question is related to this other question, which may help you: Ambiguity with inheriting an optional-parameter base method

看来你的问题与另一个问题有关,这可能会对你有所帮助:继承可选参数基本方法的歧义

#3


1  

I just installed Visual Studio 2012 RTM and the same code is working as expected even when compiled for Framework 3.5 or 2.0. So apparently this is a compiler issue, rather than .Net Framework one and has been fixed in the new version of the C# compiler.

我刚刚安装了Visual Studio 2012 RTM,即使在为Framework 3.5或2.0编译时,相同的代码也按预期工作。显然这是一个编译器问题,而不是.Net Framework 1,并且已在新版本的C#编译器中得到修复。