C# Language C# 4.0 Features Variance

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Generic interfaces and delegates can have their type parameters marked as covariant or contravariant using the out and in keywords respectively. These declarations are then respected for type conversions, both implicit and explicit, and both compile time and run time.

For example, the existing interface IEnumerable<T> has been redefined as being covariant:

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

The existing interface IComparer has been redefined as being contravariant:

public interface IComparer<in T>
{
    int Compare(T x, T y);
}


Got any C# Language Question?