开发者

Examples of good, real-life use-cases for covariance and contravariance in C# 4.0?

Before C# 4.0 came out, I was quite excited about covariance and contravariance. It pandered to my fondness for theoretical correctness!

However, now that it’s out, and I’m back to writing normal, everyday, boring code, I’m starting to wonder: did I ever use it yet? And I notice that I haven’t used it consciously. None of the interfaces or delegates I have defined since have benefited from it.

There is only one single case in which I later discovered I unwittingly used covariance, but it’s very subtle and I could as well have not noticed it. Imagine X is a base-class and Y is a descendent of it:

if (enumerableOfYs.Contains(variableOfTypeX))

Are there any interesting or exciting examples where any of you guys have used covariance or contravariance in a real-life, non-contrived use-case and it has really saved t开发者_如何转开发he day by being spectacularly “correct”?


The only "real world" use of variance that I've run into in my actual writing-programs-for-customers (as opposed to writing compiler test cases or articles or whatever) is I now can pass an IEnumerable<TypeParameterDefinedInSource> to a method that expects an IEnumerable<TypeSymbol> when I write code analysis tools in C#.

It's really not a "save the day" sort of feature. It's more of a "it works the way I expect it to" feature.


I viewed the excellent two-part video series on covariance and contravariance by Eric Lippert, one of the designers of the feature. The videos contain the clearest explanation I can find of how the feature works; however, the examples are contrived for the demonstration.

  • http://msdn.microsoft.com/en-us/vcsharp/ee672314.aspx
  • http://msdn.microsoft.com/en-us/vcsharp/ee672319.aspx

I especially appreciated Eric's discussion at the end of the second video about when he expected users would use the feature. He indicated that most line-of-business application developers probably would not create many interfaces employing covariance or contravariance, but that it is more likely the role of a framework developer (including your own frameworks, of course).

On the other hand, I can finally place an IEnumerable<DerivedClass> into a method expecting an IEnumerable<BaseClass> without jumping through type casting hoops myself. Eventually, I'll probably forget that I ever couldn't. I think this will be the most compelling and common usage of covariance/contravariance: super-subtle things that just work when, in earlier versions of .NET, they didn't.


Features like covariance and contravariance don't really "save the day." Rather, when used correctly, they make later maintenance easier. In this way, it's much like const correctness in C++. It doesn't add any power, but code written correctly uses it and it makes the system much easier to work with.

NOTE: I haven't used it yet because I just upgraded to VS 2010.


I used C# for about three years, and I only recall ever "missing" variance features maybe once or twice.

It's possible that now that they're in the language, I'll find more opportunities to take advantage of them, but I do think they are 'minor' features at best.

(I'm interested to see what others answers appear here.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜