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

前端 未结 9 983
我在风中等你
我在风中等你 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:28

    It is to do with the creation of the List, you have specified the T to be IFoo therefore you cannot instantiate it as a Bar since they are different types, even though Bar supports IFoo.

提交回复
热议问题