【发布时间】:2010-11-05 19:05:07
【问题描述】:
假设我在一个列表中有 3 个字符串(例如“1”、“2”、“3”)。
然后我想重新排列它们以将“2”放在位置 1(例如“2”、“1”、“3”)。
我正在使用此代码(将 indexToMoveTo 设置为 1):
listInstance.Remove(itemToMove);
listInstance.Insert(indexToMoveTo, itemToMove);
这似乎有效,但我偶尔会得到奇怪的结果;有时订单不正确或列表中的项目被删除!
有什么想法吗? List<T>保证订单吗?
相关:
Does a List<T> guarantee that items will be returned in the order they were added?
【问题讨论】:
-
这里提到的不是这样:stackoverflow.com/a/1790318/696517
标签: c# .net collections