Skip to content

Instantly share code, notes, and snippets.

@illegitimis
Created November 27, 2018 14:23
Show Gist options
  • Save illegitimis/19be12b460e9f46e8e96fea20d5b998d to your computer and use it in GitHub Desktop.
Save illegitimis/19be12b460e9f46e8e96fea20d5b998d to your computer and use it in GitHub Desktop.
Interface Covariance
public interface ICovariantExample<out T>
{
// Invalid variance
// The type parameter 'T' must be contravariantly valid on 'ICovariantExample<T>.Go(T)'.
// 'T' is covariant.
T Go();
}
public class CovariantExample<T> : ICovariantExample<T>
{
public T Go() { Console.WriteLine("CovariantExample {0}", typeof(T).Name); return default(T); }
}
ICovariantExample<string> sout = new CovariantExample<string>();
sout.Go();
ICovariantExample<object> oout = sout;
oout.Go();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment