Multiple ways to remove item from List in C#
♉ Remove
This method will remove first occurrence of that item , it will return true/false stating that either item was removed or not.
It is suitable in situations where you are aware that only one entry exists in your List then use this method to remove it.
♉ RemoveAt
It takes index as a parameter and removes item from that index in list, if index was invalid it will throw ArgumentOutOfRangeException.
It can be helpful when you already know that at which indexes your desired items exist to whom you want to remove.
♉ RemoveRange
This method takes two parameters of type integer. It removes a list of items from the starting index to the number of items.
It also throws ArgumentOutOfRangeException if index was out of bound or invalid.
♉ Clear
This method removes all items from the list
♉ RemoveAll
This method removes all items that meet the specific condition , we can pass it the condition and those all items would be removed. It returns the count of number of elements that were removed.
If you want to help the author in growing
- Subscribe my Weekly .NET Newsletter of C#/.NET with 250+ Software Engineers
- Follow me on LinkedIn/Twitter OR Clap at least 50 times.
- Get exclusive .NET Questions & Answers by becoming a Patron.
- Download my eBook at Gum road that contains 30 .NET Tips.