Last active
August 29, 2015 13:59
-
-
Save stormoz/10960579 to your computer and use it in GitHub Desktop.
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
namespace CovarianceTests | |
{ | |
using System.Collections.Generic; | |
using NUnit.Framework; | |
public class CovarianceTests | |
{ | |
[Test] | |
public void IEnumerableT_Is_Covariant() | |
{ | |
var list = new[] {new C1()}; | |
//var list = new[] {new C1()}.ToList(); will not compile! | |
var b = new B | |
{ | |
List = list | |
}; | |
Do(b); | |
} | |
private static void Do(A a) | |
{ | |
Assert.IsTrue(a.List[0].Val); | |
} | |
} | |
public class A | |
{ | |
public IList<C> List { get; set; } | |
} | |
public class B : A | |
{ } | |
public class C | |
{ | |
public virtual bool Val { get { return false; } } | |
} | |
public class C1 : C | |
{ | |
public override bool Val { get { return true; } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment