List<T> ক্লাসের Remove(T item) মেথড দিয়ে নির্দিষ্ট মানवाले প্রথম উপাদানটি অপসারণ করা যায়। এটি বুলিয়ান রিটার্ন করে: সফল হলে true, না পেলে false।
উদাহরণ ১: স্ট্রিং লিস্ট থেকে উপাদান মুছে ফেলা
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List list1 = new List();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
list1.Add("Five");
Console.WriteLine("List1-এ উপাদানসমূহ...");
foreach (string res in list1) {
Console.WriteLine(res);
}
List list2 = new List();
list2.Add("India");
list2.Add("US");
list2.Add("UK");
list2.Add("Canada");
list2.Add("Poland");
list2.Add("Netherlands");
Console.WriteLine("List2-এ উপাদানসমূহ...");
foreach (string res in list2) {
Console.WriteLine(res);
}
Console.WriteLine("\nList2 List1-এর সমান? = " + list2.Equals(list1));
Console.WriteLine("\nlist2-এ উপাদানের সংখ্যা = " + list2.Count);
list2.Remove("US");
Console.WriteLine("\nlist2-এ উপাদানের সংখ্যা (আপডেটেড) = " + list2.Count);
Console.WriteLine("List2-এ উপাদানসমূহ... আপডেটেড");
foreach (string res in list2) {
Console.WriteLine(res);
}
}
}
আউটপুট
List1-এ উপাদানসমূহ...
One
Two
Three
Four
Five
List2-এ উপাদানসমূহ...
India
US
UK
Canada
Poland
Netherlands
List2 List1-এর সমান? = False
list2-এ উপাদানের সংখ্যা = 6
list2-এ উপাদানের সংখ্যা (আপডেটেড) = 5
List2-এ উপাদানসমূহ... আপডেটেড
India
UK
Canada
Poland
Netherlands
উদাহরণ ২: ইন্টিজার লিস্ট থেকে উপাদান মুছে ফেলা
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List list = new List();
list.Add(5);
list.Add(10);
list.Add(20);
list.Add(50);
list.Add(75);
list.Add(100);
Console.WriteLine("List-এ উপাদানসমূহ...");
foreach (int res in list) {
Console.WriteLine(res);
}
Console.WriteLine("\nList-এ উপাদানের সংখ্যা = " + list.Count);
list.Remove(50);
Console.WriteLine("\nList-এ উপাদানের সংখ্যা (আপডেটেড) = " + list.Count);
Console.WriteLine("List-এ উপাদানসমূহ... আপডেটেড");
foreach (int res in list) {
Console.WriteLine(res);
}
}
}
আউটপুট
List-এ উপাদানসমূহ...
5
10
20
50
75
100
List-এ উপাদানের সংখ্যা = 6
List-এ উপাদানের সংখ্যা (আপডেটেড) = 5
List-এ উপাদানসমূহ... আপডেটেড
5
10
20
75
100
মূল পয়েন্ট: Remove প্রথম मैचিং উপাদানই মুছে ফেলে। সকল मैच মুছে ফেলতে RemoveAll ব্যবহার করুন। ইনডেক্স দিয়ে মুছে ফেলতে RemoveAt(index) ব্যবহার করুন।