C# List: why you cannot do `List foo = new List();`

前端 未结 9 1036
我在风中等你
我在风中等你 2020-12-02 20:05

If you have an Interface IFoo and a class Bar : IFoo, why can you do the following:

List foo = new List();          


        
9条回答
  •  忘掉有多难
    2020-12-02 20:11

    If you need to convert a list to a list of a base class or interface you can do this:

    using System.Linq;
    
    ---
    
    List bar = new List();
    bar.add(new Bar());
    
    List foo = bar.OfType().ToList();
    

提交回复
热议问题