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

发布于 2024-11-08 15:16:29 字数 375 浏览 4 评论 0原文

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

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

如果项目已成功删除,则为 true; 否则为假。这个方法还 如果在以下位置找不到项目,则返回 false 列表(T)。

他们的措辞方式让我认为对 List(Of T) 中找到的项目进行删除操作实际上可能会失败,因此出现了这个问题。

in what situation will an item in System.Collections.Generic.List not be removed successfully?

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

true if item is successfully removed;
otherwise, false. This method also
returns false if item was not found in
the List(Of T).

The way they phrase it makes me think that it is possible that a Remove operation on an item found in the List(Of T) could actually fail, hence this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就像说晚安 2024-11-15 15:16:29

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

int index = this.IndexOf(item);
if (index >= 0)
{
    this.RemoveAt(index);
    return true;
}
return false;

Looking at the System.Collections.Generic.List source in Reflector, it would appear that the item not being found in the collection is indeed the only way for Remove to return false.

int index = this.IndexOf(item);
if (index >= 0)
{
    this.RemoveAt(index);
    return true;
}
return false;
楠木可依 2024-11-15 15:16:29
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public bool Remove(T item)
{
    int index = this.IndexOf(item);
    if (index >= 0)
    {
        this.RemoveAt(index);
        return true;
    }
    return false;
}

上面的代码通过反射器。仅当不在集合中时才不会被删除。我猜测文档/语言存在差异。

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public bool Remove(T item)
{
    int index = this.IndexOf(item);
    if (index >= 0)
    {
        this.RemoveAt(index);
        return true;
    }
    return false;
}

Code above via reflector. Will only not be removed if it wasn't in the collection. I'm guessing a documentation/language discrepancy.

白云悠悠 2024-11-15 15:16:29

是的,如果您尝试删除不在列表中的项目,它会被归类为失败并返回 false 以表明您没有删除任何内容。

如果您确实删除了某些内容,然后想要执行其他代码,这可能会很有用。

更新:如果您的类实现 IEquality 并引发异常,则代码会允许引发异常,因为它没有机会返回。

再加上其他人发布反射源,只有当它找不到项目时才返回 false。

更新:进一步参考其他人的来源。如果您查看 IndexOf 方法链,您会发现它归结为相等并且没有做任何特殊的事情。

List.Remove:

public bool Remove(T item)
{
    int num = this.IndexOf(item);
    if (num >= 0)
    {
        this.RemoveAt(num);
        return true;
    }
    return false;
}

List.IndexOf:

public int IndexOf(T item)
{
    return Array.IndexOf<T>(this._items, item, 0, this._size);
}

Array.IndexOf:

public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if (startIndex < 0 || startIndex > array.Length)
    {
        throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
    }
    if (count < 0 || count > array.Length - startIndex)
    {
        throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
    }
    return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count);
}

EqualityComparer.IndexOf:

internal virtual int IndexOf(T[] array, T value, int startIndex, int count)
{
    int num = startIndex + count;
    for (int i = startIndex; i < num; i++)
    {
        if (this.Equals(array[i], value))
        {
            return i;
        }
    }
    return -1;
}

所有代码均来自 ILSpy,不感谢 Red Gate :-)

Yes it can, if you are trying to remove an item that isn't in the list - it is classed as a fail and returns false to show you that nothing was removed.

This can be useful if you then want to do other code if something was actually removed.

Update: if your class implements IEquality and that throws an exception, the code lets the throw occur, as in it doesn't get a chance to return.

Coupled with others posting the reflected source, returning false is only when it cannot find an item.

Update: further to other people's source. If you look at the IndexOf chain of methods, you will see that it boils down to equality and does nothing special.

List.Remove:

public bool Remove(T item)
{
    int num = this.IndexOf(item);
    if (num >= 0)
    {
        this.RemoveAt(num);
        return true;
    }
    return false;
}

List.IndexOf:

public int IndexOf(T item)
{
    return Array.IndexOf<T>(this._items, item, 0, this._size);
}

Array.IndexOf:

public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if (startIndex < 0 || startIndex > array.Length)
    {
        throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
    }
    if (count < 0 || count > array.Length - startIndex)
    {
        throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Count"));
    }
    return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count);
}

EqualityComparer.IndexOf:

internal virtual int IndexOf(T[] array, T value, int startIndex, int count)
{
    int num = startIndex + count;
    for (int i = startIndex; i < num; i++)
    {
        if (this.Equals(array[i], value))
        {
            return i;
        }
    }
    return -1;
}

All code from ILSpy, no thanks to Red Gate :-)

三生一梦 2024-11-15 15:16:29

在Mono的源代码中进行比较:

https://github.com/mono/mono/raw/master/mcs/class/corlib/System.Collections.Generic/List.cs

public bool Remove (T item)
{
    int loc = IndexOf (item);
    if (loc != -1)
        RemoveAt (loc);

    return loc != -1;
}

所以,文档过于模糊

In Mono's source code for comparison:

https://github.com/mono/mono/raw/master/mcs/class/corlib/System.Collections.Generic/List.cs

public bool Remove (T item)
{
    int loc = IndexOf (item);
    if (loc != -1)
        RemoveAt (loc);

    return loc != -1;
}

So, the documentation is overly fuzzy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文