C#-এ List<T> ক্লাসের RemoveRange(int index, int count) মেথড ব্যবহার করে নির্দিষ্ট ইনডেক্স থেকে শুরু করে নির্দিষ্ট সংখ্যক উপাদান একসাথে মুছে ফেলা যায়। এটি একাধিক RemoveAt() কল করার চেয়ে দ্রুত কারণ এটি একটি অপারেশনে কপি/শিফট করে।
উদাহরণ ১: ইনডেক্স ১ থেকে ৩টি উপাদান মুছে ফেলা
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> list1 = new List<string>();
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<string> list2 = new List<string>();
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);
// ইনডেক্স ১ থেকে ৩টি উপাদান মুছে ফেলা (US, UK, Canada)
list2.RemoveRange(1, 3);
Console.WriteLine("\nআপডেটেড উপাদানের সংখ্যা = " + 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 আপডেটেড উপাদানের সংখ্যা = 3 আপডেটেড List2-এর উপাদান... India Poland Netherlands
উদাহরণ ২: একাধিক RemoveRange কল করা
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> list = new List<string>();
list.Add("India");
list.Add("US");
list.Add("UK");
list.Add("Canada");
list.Add("Poland");
list.Add("Netherlands");
list.Add("Bhutan");
list.Add("Singapore");
Console.WriteLine("List-এর উপাদান...");
foreach (string res in list) {
Console.WriteLine(res);
}
Console.WriteLine("\nউপাদানের সংখ্যা = " + list.Count);
// প্রথম রেঞ্জ: ইনডেক্স ১ থেকে ৩টি (US, UK, Canada)
list.RemoveRange(1, 3);
Console.WriteLine("\nআপডেটেড উপাদানের সংখ্যা = " + list.Count);
Console.WriteLine("আপডেটেড List-এর উপাদান...");
foreach (string res in list) {
Console.WriteLine(res);
}
// দ্বিতীয় রেঞ্জ: ইনডেক্স ২ থেকে ৩টি (Netherlands, Bhutan, Singapore)
list.RemoveRange(2, 3);
Console.WriteLine("\nআবার আপডেটেড উপাদানের সংখ্যা = " + list.Count);
Console.WriteLine("আবার আপডেটেড List-এর উপাদান...");
foreach (string res in list) {
Console.WriteLine(res);
}
}
}
আউটপুট
List-এর উপাদান... India US UK Canada Poland Netherlands Bhutan Singapore উপাদানের সংখ্যা = 8 আপডেটেড উপাদানের সংখ্যা = 5 আপডেটেড List-এর উপাদান... India Poland Netherlands Bhutan Singapore আবার আপডেটেড উপাদানের সংখ্যা = 2 আবার আপডেটেড List-এর উপাদান... India Poland
ব্যাখ্যা: RemoveRange(index, count) মেথড index থেকে শুরু করে count সংখ্যক উপাদান মুছে ফেলে। প্যারামিটার ভ্যালিডেশন: index >= 0, count >= 0, এবং index + count <= Count। অপরূপভাবে ArgumentOutOfRangeException থ্রো করে। এটি বাকি উপাদানগুলো শিফট করে ইনডেক্স আপডেট করে।