Created
November 27, 2018 14:23
-
-
Save illegitimis/19be12b460e9f46e8e96fea20d5b998d to your computer and use it in GitHub Desktop.
Interface Covariance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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