Why can I not assign a List of concrete types to a List of that concrete's interface?

前端 未结 8 1516
悲哀的现实
悲哀的现实 2020-12-19 02:17

Why does this not compile?

public interface IConcrete { }

public class Concrete : IConcrete { }

public class Runner
{
    public static void Main()
    {
         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 03:11

    IList wouldn't work because IList isn't contravariant. It needs to be IEnumerable though again this only works in 4.0. You could also just use a ConvertAll with a lambda expression and that will work in 3.5

提交回复
热议问题