4

to be honest- ive asked (a part of this question) here but now i have a different - related question.

public class Base
{
    public void Foo(IEnumerable<string> strings)  { }
}

public class Child : Base
{
    public void Foo(IEnumerable<object> objects) { }
}


List<string> lst = new List<string>();
lst.Add("aaa");
Child c = new Child();
c.Foo(lst);

(n C# 3 it will call : Base.Foo in C# 4 it will call : Child.Foo)

Im in FW4 ! , lets talk about it

with all the respect to covariance : when I write c.Foo(lst); ( lst is IEnumerable of STRING !) -

it sees both signatures !!! but STILL - it chooses IEnumerable<object> ??

does covariance stronger than the concrete type itself ?

5
  • You can force it to choose one of the methods by assigning a signature. c.Foo((IEnumerable<string>)lst) in your situation, neither signature is a match for the actual concrete type so you should make no assumptions. (The answer below gives the rationale from the spec for the choice, which apparently is different from 3.0, but in these grey areas I prefer just to explicitly define the behavior with a cast) May 5, 2012 at 13:00
  • @jamietre, no, that doesn't work. Your code will still invoke the method from Child.
    – svick
    May 5, 2012 at 13:04
  • Well you've just destroyed my whole world! Sure enough. It appears that the "set of candidate methods" is reduced before disambiguation with casts. May 5, 2012 at 13:08
  • This is disturbing. Moving from C# 3.0 to C# 4.0 without doing any changes to the code could break the existing code. May 5, 2012 at 13:29
  • @OlivierJacot-Descombes yep... it is disturbing....
    – Royi Namir
    May 5, 2012 at 13:30

1 Answer 1

5

This is not because covariance is stronger, but because C# chooses the “closer” method first. So, it looks at Child.Foo(), decides it is applicable (thanks to covariance) and doesn't even look at Base.Foo().

The assumption here is that specific type “knows” more, so its methods should be considered first.

See §7.6.5.1 of the C# 4 spec:

The set of candidate methods is reduced to contain only methods from the most derived types: For each method C.F in the set, where C is the type in which the method F is declared, all methods declared in a base type of C are removed from the set.

9
  • and if i want the base func ?
    – Royi Namir
    May 5, 2012 at 13:16
  • Then you need to use a cast: either implicit by changing the variable type to Base, or explicit: ((Base)c).Foo(lst).
    – svick
    May 5, 2012 at 13:22
  • Or you can use the base keyword if you are making the call within Child: base.Foo(lst); May 5, 2012 at 13:33
  • @OlivierJacot-Descombes Error 45 Keyword 'base' is not available in a static method
    – Royi Namir
    May 5, 2012 at 13:57
  • @svick methods is reduced --- meaning in compile time...right ?
    – Royi Namir
    May 5, 2012 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.