需求:遍历List列表,当访问的元素符合某一条件时,将该元素移除出列表。

注意点:使用foreach循环遍历无法做到边读边修改,所以要使用for循环。

例子:

// 倒序遍历。
for (int i = list.Count - 1; i >= 0 ; i--)
{
    if (list[i].name != null)
    {
        // list.Remove(list[i]);
        list.Remove(i);
    }
}

重要参考:

http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2021-12-13
  • 2021-07-10
  • 2021-10-05
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2022-02-04
  • 2021-08-13
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案