docs icon indicating copy to clipboard operation
docs copied to clipboard

C# tutorials - covariance and contravariance

Open stevehoag opened this issue 11 years ago • 5 comments

Show how to enable implicit conversion of generic type parameters in interfaces and delegates in C#.

Tutorials should include one or more code examples, each example should also show expected output and should include an explanation of the code where it isn't obvious. Examples shouldn't be dependent on a specific IDE or target platform.

stevehoag avatar Oct 12 '15 20:10 stevehoag

This topic might be better covered under arrays and collections. (The collections issue is #57)

Developers may not realize they want information about Covariance and Contravariance, but they will care about the effects when those features cause bugs when working with arrays.

BillWagner avatar Feb 23 '16 17:02 BillWagner

@BillWagner what are your thoughts on explicitly discouraging the use of array covariance?

Jon Skeet's answer to a question on the topic comes to mind:

string[] strings = new string[1];
object[] objects = strings;
objects[0] = new object(); // Compiles happily, but blows up at runtime

cartermp avatar Feb 23 '16 17:02 cartermp

I agree - I'm also looking at de-emphasizing the Covariance and Contravariance topics in the MSDN docs.

stevehoag avatar Feb 23 '16 17:02 stevehoag

@cartermp Yes, array covariance should be explicitly discouraged. Now that there is safe covariance with generics, there's not a good reason.

BillWagner avatar Feb 23 '16 18:02 BillWagner

When completed, let's add a link to this from delegates-strongly-typed.md

ghogen avatar Mar 31 '16 23:03 ghogen