C#-এ StringCollection ক্লাসের RemoveAt(int index) মেথড ব্যবহার করে নির্দিষ্ট ইনডেক্সে অবস্থিত স্ট্রিং মুছে ফেলা যায়। এটি ArrayList-এর মতো কাজ করে: নির্দিষ্ট ইনডেক্সের আইটেম রিমুভ করে এবং বাকি উপাদানগুলো শিফট করে। ইনডেক্স ০-বেসড।
উদাহরণ ১: ইনডেক্স ৩ (৪র্থ উপাদান) মুছে ফেলা
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "300", "400", "500" };
Console.WriteLine("অ্যারে উপাদান...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("মোট উপাদানের সংখ্যা = " + stringCol.Count);
// ইনডেক্স ৩ (400) মুছে ফেলা
stringCol.RemoveAt(3);
Console.WriteLine("এখন মোট উপাদানের সংখ্যা = " + stringCol.Count);
}
}
আউটপুট
অ্যারে উপাদান... 100 200 300 400 500 মোট উপাদানের সংখ্যা = 5 এখন মোট উপাদানের সংখ্যা = 4
উদাহরণ ২: একাধিক ইনডেক্স থেকে মুছে ফেলা
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "A", "B", "C", "D", "E" };
Console.WriteLine("অ্যারে উপাদান...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("মোট উপাদানের সংখ্যা = " + stringCol.Count);
// ইনডেক্স ১ (B) মুছে ফেলা
stringCol.RemoveAt(1);
// আপডেটেড লিস্টে ইনডেক্স ২ (D) মুছে ফেলা
stringCol.RemoveAt(2);
Console.WriteLine("এখন মোট উপাদানের সংখ্যা = " + stringCol.Count);
}
}
আউটপুট
অ্যারে উপাদান... A B C D E মোট উপাদানের সংখ্যা = 5 এখন মোট উপাদানের সংখ্যা = 3
ব্যাখ্যা: RemoveAt(index) নির্দিষ্ট ইনডেক্সের উপাদান মুছে ফেলে। ইনডেক্স অবৈধ হলে ArgumentOutOfRangeException থ্রো করে। একাধিক রিমুভ করার সময় ইনডেক্স পরিবর্তন হয় বলে সতর্ক থাকতে হয় (উদাহরণ ২-তে দেখানো হয়েছে)। StringCollection লিগ্যাসি ক্লাস; নতুন প্রজেক্টে List<string> ব্যবহার করুন যেটে RemoveAt, Remove, RemoveAll ইত্যাদি সমৃদ্ধ মেথড রয়েছে।