Search code examples
c#c#-4.0covariancecontravariance

Why do we need new keywords for Covariance and Contravariance in C#?


Can someone explain why there is the need to add an out or in parameter to indicate that a generic type is Co or Contra variant in C# 4.0?

I've been trying to understand why this is important and why the compiler can't just figure it out..

Thanks,

Josh


Solution

  • Eric Lippert, who works on the langauge, has a series of posts on msdn that should help clarify the issues involved:
    http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

    When reading the articles shown at that link, start at the bottom and work up.

    Eventually you'll get to #7 (Why do we need a syntax at all?).