Essential C# C# 4.0 Features  Suggest an edit

Variance

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 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);
}


Table Of Contents
22 Enum
25 GUID
73 C# 4.0 Features
107 Stream
108 Timers
  ↑ ↓ to navigate     ↵ to select     Esc to close