dtb*_*dtb 26
该名单<T>类并不提供这样的方法,但你可以编写获取项目的扩展方法,删除它终于重新插入它:
static class ListExtensions
{
static void MoveItemAtIndexToFront<T>(this List<T> list, int index)
{
T item = list[index];
list.RemoveAt(index);
list.Insert(0, item);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,3个答案中的任何一个都有诀窍,但我建议不要执行RemoveAt和Insert操作,而是建议将每个项目从左侧所需的位置向右移动到列表的开头.这样您就可以避免移动放置在项目右侧的项目.
这是@ dtb答案的修改.
static class ListExtensions
{
static void MoveItemAtIndexToFront<T>(this List<T> list, int index)
{
T item = list[index];
for (int i = index; i > 0; i--)
list[i] = list[i - 1];
list[0] = item;
}
}
Run Code Online (Sandbox Code Playgroud)
var l = new List<DataItem>();
var temp = l[index];
l.RemoveAt(index);
l.Insert(0, temp);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17436 次 |
| 最近记录: |