Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/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.

like image 218
Pacerier Avatar asked May 20 '11 10:05

Pacerier


People also ask

What is the use of using System collections generic?

Contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

How does list remove work c#?

The Remove method removes the first occurrence of a specific object from a List. The Remove method takes an item as its parameter. We can use the RemoveAt method to remove an item at the specified position within a List. The Remove method removes the first occurrence of a specific object from a List.


1 Answers

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;
like image 180
Kevin Wienhold Avatar answered Nov 15 '22 15:11

Kevin Wienhold