Is there a trick in creating a generic list of anonymous type?

前端 未结 9 1690
有刺的猬
有刺的猬 2021-01-30 05:25

Sometimes i need to use a Tuple, for example i have list of tanks and their target tanks (they chase after them or something like that ) :

List

        
9条回答
  •  醉梦人生
    2021-01-30 05:59

    You could just have a generic method that takes a params and returns it:

    public static IList ToAnonymousList(params T[] items)
    {
      return items;
    }
    

    So now you can say:

    var list=ToAnonymousList
    (
      new{A=1, B=2},
      new{A=2, B=2}
    );
    

提交回复
热议问题