Adding different type of generic objects into generic list

前端 未结 4 1899
星月不相逢
星月不相逢 2020-12-03 22:01

Is it possible to add different type of generic objects to a list?. As below.

public class ValuePair
{        
    public string Name { get; set;}
          


        
4条回答
  •  再見小時候
    2020-12-03 22:42

    it's not possible as far as I know.

    the line:

    List list = new List();
    

    you wrote in your sample is not providing a concrete type for T and this is the issue, once you pass it, you can only add object of that specific type.

提交回复
热议问题