c# - 如何更新List中item的索引?

标签 c# .net list

我有以下列表:

var list = new List<string>(); 

//

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

做点什么..

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

我正在寻找替代方案:

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

最佳答案

别无选择,这就是你要做的。如果您想要提高一点效率并且知道索引,那么您可以使用 RemoveAt:

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

关于c# - 如何更新List中item的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8774743/

相关文章:

c# - 将所有标记的字符串更改为另一个字符串

c# - 排序通用列表

python - 在列表中找到 int 后删除索引 - Python

c# - 如何动态构建与 Linq 和 Entity Framework 相关的 Select

c# - 如何在 case 语句中使用 C# 枚举值的字符串值?

c# - 对另一个 LINQ 查询的 LINQ 查询

c# - 升级到 CaSTLe 3.0 后出现 ComponentResolutionException

.net - 两个 SELECTS,一个查询

java - 如何在文件列表中搜索文件名

python - 访问列表中元组中的值