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

前端 未结 8 1505
悲哀的现实
悲哀的现实 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条回答
  •  死守一世寂寞
    2020-12-19 02:55

    foreach (var item in myList)
        DoStuffWithInterface(item);
    

    or

    public void DoStuffWithInterface(IList concrete) { }
    

    or

    var myNewList = myList.Cast();
    

提交回复
热议问题