Generic List - moving an item within the list

前端 未结 10 686
后悔当初
后悔当初 2020-11-28 04:32

So I have a generic list, and an oldIndex and a newIndex value.

I want to move the item at oldIndex, to newIndex.

10条回答
  •  难免孤独
    2020-11-28 05:07

    List.Remove() and List.RemoveAt() do not return the item that is being removed.

    Therefore you have to use this:

    var item = list[oldIndex];
    list.RemoveAt(oldIndex);
    list.Insert(newIndex, item);
    

提交回复
热议问题