在什么情况下,System.Collections.Generic.List中的项目不能成功删除?

Pac*_*ier 13 .net c# vb.net list generic-list

在什么情况下,System.Collections.Generic.List中的项目不能成功删除?

来自http://msdn.microsoft.com/en-us/library/cd666k3e.aspx:

如果项目已成功删除,则为true; 否则,错误.如果在List(Of T)中找不到项,则此方法也返回false.

他们用短语表达的方式让我觉得List(Of T)中找到的项目的删除操作可能实际上失败了,因此这个问题.

小智 5

查看Reflector中的System.Collections.Generic.List源代码,看起来集合中未找到的项目确实是Remove返回false的唯一方法.

int index = this.IndexOf(item);
if (index >= 0)
{
    this.RemoveAt(index);
    return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)