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 :-)