Skip to content

Instantly share code, notes, and snippets.

@stormoz
Last active August 29, 2015 13:59
Show Gist options
  • Save stormoz/10960579 to your computer and use it in GitHub Desktop.
Save stormoz/10960579 to your computer and use it in GitHub Desktop.
Covariance
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