Having List<string> paths = new List<string>();
I want to remove item that I'm not sure is there. Should I check if it exists or just run the Remove method straight ahead?
Is if (paths.Exists(stringVar))
needed or considered a good practice before paths.Remove(stringVar)
?
Running Remove without Exists would simply return false in case there is no such item in list.
RemoveAt (Int32) Method is used to remove the element at the specified index of the List<T>. Properties of List: It is different from the arrays.
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.
The best way to remove items from a list while iterating over it is to use RemoveAll() .
No it doesn't throw an exception, and there is no need for extra checking . see MSDN:
true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the List.
No, don't check. Because Remove
already does the check. Your extra check is simply superfluous and brings no benefit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With