简体   繁体   English

如何更新列表中项目的索引?

[英]how to update the index of item in List?

I have the following list: 我有以下清单:

var list = new List<string>(); 

//

list.Add("foo");
list.Add("baa");

do something.. 做一点事..

if(foo) {
  //how to put baa in as first element in list?
}

I'm looking for alternative for this: 我正在寻找替代方案:

string item = "baa";
docTypes.Remove(item);
docTypes.Insert(0, item);

There is no alternative, that's how you do it. 别无选择,这就是您的方式。 If you want a bit of an efficiency improvement and you know the index, then you could use RemoveAt : 如果您想提高效率并了解索引,则可以使用RemoveAt

int index = 1;
docTypes.Insert(0, docTypes[index]);
docTypes.RemoveAt(index + 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM