Cast List to List in .NET 2.0

后端 未结 8 2123
抹茶落季
抹茶落季 2020-12-07 15:28

Can you cast a List to List somehow?

I know I could loop through and .ToString() the thing, but a cast would be aw

相关标签:
8条回答
  • 2020-12-07 16:11

    You wouldn't be able to directly cast it as no explicit or implicit cast exists from int to string, it would have to be a method involving .ToString() such as:-

    foreach (int i in intList) stringList.Add(i.ToString());
    

    Edit - or as others have pointed out rather brilliantly, use intList.ConvertAll(delegate(int i) { return i.ToString(); });, however clearly you still have to use .ToString() and it's a conversion rather than a cast.

    0 讨论(0)
  • 2020-12-07 16:11

    Converting from int List to string List can be done in two adittional ways besides the usual ToString(). Choose the one that pleases you more.

    var stringlist = intlist.Select(x=>""+x).ToList();
    

    Or also:

    var stringlist = intlist.Select(x=>$"{x}").ToList();
    

    And finally the traditional:

    var stringlist = intlist.Select(x=>x.ToString()).ToList();
    
    0 讨论(0)
提交回复
热议问题