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

前端 未结 8 1513
悲哀的现实
悲哀的现实 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:54

    You could try

    public void DoStuffWithInterface(IList concrete) { }
    

    but I think this only works in .NET 4.0.

    If you wanna be dirty just do

    public void DoStuffWithInterface(IList concrete) { }
    

    and check to see if the objects coming out are concrete.

提交回复
热议问题