Why generic interfaces are not co/contravariant by default?

Viewed 2635

For example IEnumerable<T> interface:

public interface IEnumerable<out T> : IEnumerable
{
    IEnumerator<T> GetEnumerator();
}

In this interface the generic type is used only as a return type of interface method and not used as a type of method arguments thus it can be covariant. Giving this, can't compiler theoretically infer the variance from the interface? If it can, why does C# requires us to set co/contravariance keywords explicitly.

Update: As Jon Skeet mentioned this question can be spited into sub-questions:

  1. Can compiler infer generic type's co/contravariance by how it is used inside current generic type and all it's base types?

    For example.. How many generic interface parameters from .NET Framework 4.0 can be marked co/contravariant automatically without any ambiguity? About 70%, 80%, 90% or 100%?

  2. If it can, should it apply co/contravariance to generic types by default? At least to those types which it is capable to analyze and infer co/contravariance from the type usage.

2 Answers
Related