C# 如何更新列表中项目的索引?
我有以下清单:C# 如何更新列表中项目的索引?,c#,.net,list,C#,.net,List,我有以下清单: var list = new List<string>(); // list.Add("foo"); list.Add("baa"); 我正在为此寻找替代方案: string item = "baa"; docTypes.Remove(item); docTypes.Insert(0, item); 别无选择,你就是这样做的。如果您想提高一点效率,并且知道索引,那么可以使用RemoveAt: int index = 1; docTypes.Insert(0,
var list = new List<string>();
//
list.Add("foo");
list.Add("baa");
我正在为此寻找替代方案:
string item = "baa";
docTypes.Remove(item);
docTypes.Insert(0, item);
别无选择,你就是这样做的。如果您想提高一点效率,并且知道索引,那么可以使用
RemoveAt
:
int index = 1;
docTypes.Insert(0, docTypes[index]);
docTypes.RemoveAt(index + 1);
int index = 1;
docTypes.Insert(0, docTypes[index]);
docTypes.RemoveAt(index + 1);