问题
Seems like in .NET Framework there is an issue with optional parameters when you override the method. The output of the code below is: "bbb" "aaa" . But the output I'm expecting is: "bbb" "bbb" .Is there a solution for this. I know it can be solved with method overloading but wondering the reason for this. Also the code works fine in Mono.
class Program
{
class AAA
{
public virtual void MyMethod(string s = "aaa")
{
Console.WriteLine(s);
}
public virtual void MyMethod2()
{
MyMethod();
}
}
class BBB : AAA
{
public override void MyMethod(string s = "bbb")
{
base.MyMethod(s);
}
public override void MyMethod2()
{
MyMethod();
}
}
static void Main(string[] args)
{
BBB asd = new BBB();
asd.MyMethod();
asd.MyMethod2();
}
}
回答1:
One thing worth noting here, is that the overridden version is called each time. Change the override to:
public override void MyMethod(string s = "bbb")
{
Console.Write("derived: ");
base.MyMethod(s);
}
And the output is:
derived: bbb
derived: aaa
A method in a class can do one or two of the following:
- It defines an interface for other code to call.
- It defines an implementation to execute when called.
It may not do both, as an abstract method does only the former.
Within BBB
the call MyMethod()
calls a method defined in AAA
.
Because there is an override in BBB
, calling that method results in an implementation in BBB
being called.
Now, the definition in AAA
informs calling code of two things (well, a few others too that don't matter here).
- The signature
void MyMethod(string)
. - (For those languages that support it) the default value for the single parameter is
"aaa"
and therefore when compiling code of the formMyMethod()
if no method matchingMyMethod()
can be found, you may replace it with a call to `MyMethod("aaa").
So, that's what the call in BBB
does: The compiler sees a call to MyMethod()
, doesn't find a method MyMethod()
but does find a method MyMethod(string)
. It also sees that at the place where it is defined there's a default value of "aaa", so at compile time it changes this to a call to MyMethod("aaa")
.
From within BBB
, AAA
is considered the place where AAA
's methods are defined, even if overridden in BBB
, so that they can be over-ridden.
At run-time, MyMethod(string)
is called with the argument "aaa". Because there is a overridden form, that is the form called, but it is not called with "bbb" because that value has nothing to do with the run-time implementation but with the compile-time definition.
Adding this.
changes which definition is examined, and so changes what argument is used in the call.
Edit: Why this seems more intuitive to me.
Personally, and since I'm talking of what is intuitive it can only be personal, I find this more intuitive for the following reason:
If I was coding BBB
then whether calling or overriding MyMethod(string)
, I'd think of that as "doing AAA
stuff" - it's BBB
s take on "doing AAA
stuff", but it's doing AAA
stuff all the same. Hence whether calling or overriding, I'm going to be aware of the fact that it was AAA
that defined MyMethod(string)
.
If I was calling code that used BBB
, I'd think of "using BBB
stuff". I might not be very aware of which was originally defined in AAA
, and I'd perhaps think of this as merely an implementation detail (if I didn't also use the AAA
interface nearby).
The compiler's behaviour matches my intuition, which is why when first reading the question it seemed to me that Mono had a bug. Upon consideration, I can't see how either fulfils the specified behaviour better than the other.
For that matter though, while remaining at a personal level, I'd never use optional parameters with abstract, virtual or overridden methods, and if overriding someone else's that did, I'd match theirs.
回答2:
You can disambiguate by calling:
this.MyMethod();
(in MyMethod2()
)
Whether it is a bug is tricky; it does look inconsistent, though. Resharper warns you simply not to have changes to the default value in an override, if that helps ;p Of course, resharper also tells you the this.
is redundant, and offers to remove it for you ... which changes the behaviour - so resharper also isn't perfect.
It does look like it could qualify as a compiler bug, I'll grant you. I'd need to look really carefully to be sure... where's Eric when you need him, eh?
Edit:
The key point here is the language spec; let's look at §7.5.3:
For example, the set of candidates for a method invocation does not include methods marked override (§7.4), and methods in a base class are not candidates if any method in a derived class is applicable (§7.6.5.1).
(and indeed §7.4 clearly omits override
methods from consideration)
There's some conflict here.... it states that the base methods are not used if there is an applicable method in a derived class - which would lead us to the derived method, but at the same time, it says that methods marked override
are not considered.
But, §7.5.1.1 then states:
For virtual methods and indexers defined in classes, the parameter list is picked from the most specific declaration or override of the function member, starting with the static type of the receiver, and searching through its base classes.
and then §7.5.1.2 explains how the values are evaluated at the time of the invoke:
During the run-time processing of a function member invocation (§7.5.4), the expressions or variable references of an argument list are evaluated in order, from left to right, as follows:
...(snip)...
When arguments are omitted from a function member with corresponding optional parameters, the default arguments of the function member declaration are implicitly passed. Because these are always constant, their evaluation will not impact the evaluation order of the remaining arguments.
This explicitly highlights that it is looking at the argument list, which was previously defined in §7.5.1.1 as coming from the most specific declaration or override. It seems reasonable that this is the "method declaration" that is referred to in §7.5.1.2, thus the value passed should be from the most derived up-to the static type.
This would suggest: csc has a bug, and it should be using the derived version ("bbb bbb") unless it is restricted (via base.
, or casting to a base-type) to looking at the base method declarations (§7.6.8).
回答3:
This looks like a bug to me. I believe it is well specified,
and that it should behave in the same way as if you call the
method with the explicit this
prefix.
I've simplified the example to only use a single virtual method, and show both which implementation is called and what the parameter value is:
using System;
class Base
{
public virtual void M(string text = "base-default")
{
Console.WriteLine("Base.M: {0}", text);
}
}
class Derived : Base
{
public override void M(string text = "derived-default")
{
Console.WriteLine("Derived.M: {0}", text);
}
public void RunTests()
{
M(); // Prints Derived.M: base-default
this.M(); // Prints Derived.M: derived-default
base.M(); // Prints Base.M: base-default
}
}
class Test
{
static void Main()
{
Derived d = new Derived();
d.RunTests();
}
}
So all we need to worry about are the three calls within RunTests. The important bits of the spec for the first two calls are section 7.5.1.1, which talks about the parameter list to be used when finding corresponding parameters:
For virtual methods and indexers defined in classes, the parameter list is picked from the most specific declaration or override of the function member, starting with the static type of the receiver, and searching through its base classes.
And section 7.5.1.2:
When arguments are omitted from a function member with corresponding optional parameters, the default arguments of the function member declaration are implicitly passed.
The "corresponding optional parameter" is the bit that ties 7.5.2 to 7.5.1.1.
For both M()
and this.M()
, that parameter list should be
the one in Derived
as static type of the receiver is Derived
,
Indeed, you can tell that the compiler treats
that as the parameter list earlier in the compilation, as if you
make the parameter mandatory in Derived.M()
, both of the
calls fail - so the M()
call requires the parameter to have
a default value in Derived
, but then ignores it!
Indeed, it gets worse: if you provide a default value for the
parameter in Derived
but make it mandatory in Base
, the call
M()
ends up using null
as the argument value. If nothing else,
I'd say that proves it's a bug: that null
value can't come
from anywhere valid. (It's null
due to that being the default
value of the string
type; it always just uses the default value
for the parameter type.)
Section 7.6.8 of the spec deals with base.M(), which says that
as well as the non-virtual behaviour, the expression is considered
as ((Base) this).M()
; so it's entirely correct for the base method
to be used to determine the effective parameter list. That means
the final line is correct.
Just to make things easier for anyone who wants to see the really odd bug described above, where a value not specified anywhere is used:
using System;
class Base
{
public virtual void M(int x)
{
// This isn't called
}
}
class Derived : Base
{
public override void M(int x = 5)
{
Console.WriteLine("Derived.M: {0}", x);
}
public void RunTests()
{
M(); // Prints Derived.M: 0
}
static void Main()
{
new Derived().RunTests();
}
}
回答4:
Have you tried:
public override void MyMethod2()
{
this.MyMethod();
}
So you actually tell your program to use the overriden Method.
回答5:
The behaviour is definitely very strange; it is not clear to me if it is in fact a bug in the compiler, but it might be.
The campus got a fair amount of snow last night and Seattle is not very good about dealing with snow. My bus is not running this morning so I'm not going to be able to get into the office to compare what C# 4, C# 5 and Roslyn say about this case and if they disagree. I'll try to post an analysis later this week once I'm back in the office and can use proper debugging tools.
回答6:
May be this is due to ambiguity and the compiler is giving priority to the base/super class. The below change to code of your class BBB with adding reference to this
keyword, gives the output 'bbb bbb':
class BBB : AAA
{
public override void MyMethod(string s = "bbb")
{
base.MyMethod(s);
}
public override void MyMethod2()
{
this.MyMethod(); //added this keyword here
}
}
One of the things it implies is you should always use the this
keyword whenever you are calling properties or methods on the current instance of class as a best practice.
I would be concerned if this ambiguity in base and child method didn't even raise a compiler warning (if not error), but if it does then that was unseen I suppose.
==================================================================
EDIT: Consider below sample excerpts from these links:
http://geekswithblogs.net/BlackRabbitCoder/archive/2011/07/28/c.net-little-pitfalls-default-parameters-are-compile-time-substitutions.aspx
http://geekswithblogs.net/BlackRabbitCoder/archive/2010/06/17/c-optional-parameters---pros-and-pitfalls.aspx
Pitfall: Optional parameter values are compile-time There is one thing and one thing only to keep in mind when using optional parameters. If you keep this one thing in mind, chances are you may well understand and avoid any potential pitfalls with their usage: That one thing is this: optional parameters are compile-time, syntactical sugar!
Pitfall: Beware of Default Parameters in Inheritance and Interface Implementation
Now, the second potential pitfalls has to do with inheritance and interface implementation. I’ll illustrate with a puzzle:
1: public interface ITag
2: {
3: void WriteTag(string tagName = "ITag");
4: }
5:
6: public class BaseTag : ITag
7: {
8: public virtual void WriteTag(string tagName = "BaseTag") { Console.WriteLine(tagName); }
9: }
10:
11: public class SubTag : BaseTag
12: {
13: public override void WriteTag(string tagName = "SubTag") { Console.WriteLine(tagName); }
14: }
15:
16: public static class Program
17: {
18: public static void Main()
19: {
20: SubTag subTag = new SubTag();
21: BaseTag subByBaseTag = subTag;
22: ITag subByInterfaceTag = subTag;
23:
24: // what happens here?
25: subTag.WriteTag();
26: subByBaseTag.WriteTag();
27: subByInterfaceTag.WriteTag();
28: }
29: }
What happens? Well, even though the object in each case is SubTag whose tag is “SubTag”, you will get:
1: SubTag 2: BaseTag 3: ITag
But remember to make sure you:
Do not insert new default parameters in the middle of an existing set of default parameters, this may cause unpredictable behavior that may not necessarily throw a syntax error – add to end of list or create new method. Be extremely careful how you use default parameters in inheritance hierarchies and interfaces – choose the most appropriate level to add the defaults based on expected usage.
==========================================================================
回答7:
This I think is because these default values are fixed at the compile time. If you use reflector you will see the following for MyMethod2 in BBB.
public override void MyMethod2()
{
this.MyMethod("aaa");
}
回答8:
Agree in general with @Marc Gravell.
However, I'd like to mention that the issue is old enough in C++ world (http://www.devx.com/tips/Tip/12737), and the answer looks like "unlike virtual functions, which are resolved at run time, default arguments are resolved statically, that is, at compiled time." So this C# compiler behavior had rather been accepted deliberately due to consistency, despite its unexpectedness, it seems.
回答9:
Either Way It Needs A Fix
I would definitely regard it as a bug, either because the results is wrong or if the results are expected then the compiler should not let you declare it as "override", or at least provide a warning.
I would recommend you to report this to Microsoft.Connect
But Is It Right Or Wrong?
However regarding whether this is the expected behavior or not, let us first analyze the two views on it.
consider we have the following code:
void myfunc(int optional = 5){ /* Some code here*/ } //Function implementation
myfunc(); //Call using the default arguments
There are two ways to implement it:
That optional arguments are treated like overloaded functions, resulting in the following:
void myfunc(int optional){ /* Some code here*/ } //Function implementation void myfunc(){ myfunc(5); } //Default arguments implementation myfunc(); //Call using the default arguments
That the default value is embedded in the caller, thus resulting in the following code:
void myfunc(int optional){ /* Some code here*/ } //Function implementation myfunc(5); //Call and embed default arguments
There are many differences between the two approaches, but we will first take a look on how the .Net framework interprets it.
In .Net you can only override a method with a method that contains the same number of arguments, but you cannot override with a method containing more arguments, even if they are all optional (which would result in a call haveing the same signature as the overridden method), say for example you have:
class bassClass{ public virtual void someMethod()} class subClass :bassClass{ public override void someMethod()} //Legal //The following is illegal, although it would be called as someMethod(); //class subClass:bassClass{ public override void someMethod(int optional = 5)}
You can overload a method with default arguments with another method with no arguments, (this has disastrous implications as I will discuss in a moments), so the folloing code is legal:
void myfunc(int optional = 5){ /* Some code here*/ } //Function with default void myfunc(){ /* Some code here*/ } //No arguments myfunc(); //Call which one?, the one with no arguments!
when using reflection one must always provide a default value.
All of which are enough to prove that .Net took the second implementation, so the behavior that the OP saw is right, at least according to .Net.
Problems With the .Net Approach
However there are real problems with the .Net approach.
Consistency
As in the OP's problem when overriding the default value in an inherited method, then results might be unpredictable
When the original implantation of the default value is changed, and since the callers don't have to get recompiled, we might end up with default values that are no longer valid
- Reflection requires you to provide the default value, which the caller doesn't have to know
Breaking code
When we have a function with default arguments and latter we add a function with no arguments, all calls will now route to the new function, thus breaking all existing code, without any notification or warning!
Similar will happen, if we later take away the function with no arguments, then all calls will automatically route to the function with the default arguments, again with no notification or warning! although this might not be the intention of the programmer
Furthermore it does not have to be regular instance method, an extension method will do the same problems, since an extension method with no parameters will take precedence over an instance method with default parameters!
Summary: STAY AWAY FROM OPTIONAL ARGUMENTS, AND USE INSTEAD OVERLOADS (AS THE .NET FRAMEWORK ITSELF DOES)
来源:https://stackoverflow.com/questions/8909811/c-sharp-optional-parameters-on-overridden-methods