Multiple Generics ambiguity

后端 未结 2 1724
名媛妹妹
名媛妹妹 2021-01-06 06:33

The codes below are exactly the same, except that one is C# and the other one is VB.Net. C# compiles just fine, but VB.Net throws the warning:

Interfa

2条回答
  •  不要未来只要你来
    2021-01-06 07:23

    Summing up:

    • VB appears to be giving the warning unnecessarily here. I'll mention it to the VB testers when they're back from Christmas vacation.
    • This is a suspicious programming practice regardless of whether it is safe or not; it's a bit strange to implement two versions of the same interface.
    • If instead you chose a covariant interface like IEnumerable then the warning would be justified. If you have an object that is both a sequence of Turtles and a sequence of Giraffes, then what happens when you implicitly convert it to sequence of Animal? Do you get Turtles or Giraffes? The runtime just picks one, which is not necessarily the behaviour you want.

    For some interesting discussion of the last point see the comments to my 2007 article on the subject:

    http://blogs.msdn.com/b/ericlippert/archive/2007/11/09/covariance-and-contravariance-in-c-part-ten-dealing-with-ambiguity.aspx

提交回复
热议问题