📅  最后修改于: 2023-12-03 14:59:40.369000             🧑  作者: Mango
When working with lists in C#, you may need to remove an item from the list by its index. This can be accomplished easily using the List<T>.RemoveAt
method.
public void RemoveAt(int index);
The RemoveAt
method takes the index of the item to be removed as input.
Here's an example of how to use the RemoveAt
method:
List<string> fruits = new List<string>();
fruits.Add("apple");
fruits.Add("banana");
fruits.Add("orange");
Console.WriteLine("Before removal:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
fruits.RemoveAt(1);
Console.WriteLine("After removal:");
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
In this example, we create a list of fruits and add three items to it. We then print out the list before removing an item at index 1 (the second item). Finally, we print out the list again to show that the item has been removed.
The List<T>.RemoveAt
method is a useful tool when working with lists in C#. It allows you to remove an item from the list by its index, which can be helpful when you need to reorder or modify the list.