如何在同一位置替换集合列表中的值?
0 = cat
1 = dog
2 = bird
将2
替换为snail
?
答案 0 :(得分:98)
你的意思是:
yourCollection[2] = "Snail";
答案 1 :(得分:84)
在较大的List<T>
集合中,您希望找到要替换的索引...:
var i = animals.FindIndex(x => x == "Dog");
animals[i] = "Snail";
答案 2 :(得分:-21)
List<string> animals = new List<string>();
animals.Add("cat");
animals.Add("dog");
animals.Add("bird");
animals.Add("fish");
animals.RemoveAt(2);
animals.Insert(2, "snail");