인덱스를 사용하여 C#의 목록에서 항목을 제거하려면 RemoveAt() 메서드를 사용합니다.
먼저 목록을 설정하십시오 -
List<string> list1 = new List<string>() {
"Hanks",
"Lawrence",
"Beckham",
"Cooper",
}; 이제 두 번째 위치, 즉 인덱스 1에서 요소를 제거합니다.
list1.RemoveAt(1);
전체 예를 살펴보겠습니다 -
예시
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> list1 = new List<string>() {
"Hanks",
"Lawrence",
"Beckham",
"Cooper",
};
Console.Write("Initial list...");
foreach (string list in list1) {
Console.WriteLine(list);
}
Console.Write("Removing element from the list...");
list1.RemoveAt(1);
foreach (string list in list1) {
Console.WriteLine(list);
}
}
} 출력
Initial list... Hanks Lawrence Beckham Cooper Removing element from the list... Hanks Beckham Cooper