সূচী ব্যবহার করে C# এ একটি তালিকা থেকে একটি আইটেম সরাতে, RemoveAt() পদ্ধতি ব্যবহার করুন।
প্রথমত, তালিকা −
সেট করুনList<string> list1 = new List<string>() {
"Hanks",
"Lawrence",
"Beckham",
"Cooper",
}; এখন 2য় অবস্থানে থাকা উপাদানটিকে সরিয়ে দিন অর্থাৎ সূচক 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