mercredi 28 mai 2014

c# - valeur de paramètre par défaut dans les méthodes substituées - Stack Overflow


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?


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



Looks like this is a type of bug.


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


C# optional parameters on overridden methods




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




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.



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?


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


Looks like this is a type of bug.


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


C# optional parameters on overridden methods



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



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.


0 commentaires:

Enregistrer un commentaire