remove item from list c# code example

Example 1: how to delete from a list c#

var resultList = new List<int>();
resultList.Add(1);
resultList.Add(2);
resultList.Add(3);

// Removes the number 1 from the index 0 in the list
resultlist.RemoveAt(0);

// Allows you to remove an Object from the list instead
var itemToRemove = resultlist.Single(r => r.Id == 2);
resultList.Remove(itemToRemove);

Example 2: c# remove item from list

list.Remove("Example String"); // Remove by value
list.RemoveAt(3); // Remove at index
list.RemoveRange(6, 3); // Remove range (removes 3 items starting at 6th position in this example)

Example 3: remove first object from list c#

listName.RemoveAt(0);

Example 4: how to remove from list from index c#

List.Remove("ElementTypeAndValue");
// Or remove from index using:
List.Remove(List[index]);

Example 5: c# delete list

ListName.Clear();