在调用 list.Remove 方法之后,list 序列中被移除的元素会被删除,而其他元素的顺序不会改变。如果需要重新排序 list 序列中的元素,可以调用 list.Sort 方法。例如:

List<int> list = new List<int> { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
list.Remove(5); // 移除第一个值为 5 的元素
list.Sort(); // 重新排序 list 序列中的元素
foreach (int i in list)
{
    Console.WriteLine(i);
}

输出结果为:

1
1
2
3
3
4
6
9
C# List.Remove 后重新排序元素

原文地址: https://www.cveoy.top/t/topic/otrL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录