views:

743

answers:

6

I watched a video about it on Channel 9 but I didn't really understand it much.

Can someone please give me a simple example about these that's easy to understand? After that maybe how it would be used in practice?

+6  A: 

You may want to look at this blog, he does a fantastic job of explaining it, but I think it will take more examples to clear it up for people, as this gets into a very hard-to-understand area, but, the quote below from the article sums it up well.

http://hestia.typepad.com/flatlander/2008/12/c-covariance-and-contravariance-by-example.html

"covariance and contravariance" means that you can now pass inexact generic types when it's safe to do so, just as you can pass inexact argument types when it's safe to do so.

James Black
+2  A: 

http://stackoverflow.com/questions/1078423/c-is-variance-covariance-contravariance-another-word-for-polymorphism should answer a lot of your questions about it (also see Jon Skeet's link to Eric Lippert's blog.

Matt Ball
+1  A: 

The following article deals with co- and contravariance with delegates: http://msdn.microsoft.com/en-us/library/ms173174.aspx.

Perhaps it is useful to you, even if you're not into delegates yet. I found it to be quite easy to understand.

Webleeuw
+4  A: 

I also thought this video by Eric Lippert was very clear:

Part 1: http://msdn.microsoft.com/en-us/vcsharp/ee672314.aspx

Part 2: http://msdn.microsoft.com/en-us/vcsharp/ee672319.aspx

It also brings up an interesting case that I had never heard about before, about unsafe covariance with arrays.

JulianR
A: 

MSDN docs for C# 4.0 (and VB) are here: Covariance and Contravariance

If you are interested in concrete examples, they are here:

Alexandra Rusina
A: 

Eric Lippert came up with a very good explanation in a recent blog post

Thomas Levesque