开发者

How to replace value in list at same collection location [duplicate]

This question already has answers here: How to replace list item in best way (12 answers) 开发者_Python百科 Closed 5 years ago.

How do I replace a value in a collection list at the same location?

0 = cat
1 = dog
2 = bird

replace 2 with snail?


Do you mean:

yourCollection[2] = "Snail";


In a bigger List<T> collection, you would like to find index to replace...with:

 var i = animals.FindIndex(x => x == "Dog");
 animals[i] = "Snail";


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜