in what situation will an item in System.Collections.Generic.List not be removed successfully?
in what situation will an item in System.Collections.Generic.List not be removed successfully?
From http://msdn.microsoft.com/en-us/libra开发者_如何转开发ry/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.
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;
[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.
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 :-)
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
精彩评论