अनुक्रमणिका का उपयोग करके 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