Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News C# Feature Focus: Co- and Contra-variance

C# Feature Focus: Co- and Contra-variance


In order to match Java and support J#, .NET arrays have always been co-variant. Unfortunately it was done so in an unsafe fashion, necessitating the need for runtime checks whenever storing values in an array. For .NET 2 and generics, neither co- nor contra-variance was supported.

.NET 4 will support safe co- and contra-variance on interfaces and delgates. In order to do this types in the interface can be marked with either an "in" or "out" modifier. If marked as out, the interface supports co-variance and the given type can only appear as a return type or read-only property. If marked in, the type can only appear as a parameter or set-only property.

This support is only for reference types, no variance is allowed on structures. Also, parameters passed by reference are not allowed. IEnumerable, IQueryable, Func, and Action are examples of primary interfaces and delegates that will be marked for variance support.

For more information on this see our article Covariance and Contravariance in .NET Generics and Eric Lippert's Co- and contra-variance: how do I convert a List(Of Apple) into a List(Of Fruit).

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.