StringCollection ক্লাস (নেমস্পেস: System.Collections.Specialized) শুধু স্ট্রিং স্টোর করার জন্য ডিজাইন করা। Add(string) মেথড দিয়ে স্ট্রিং শেষে যোগ করা যায়।
উদাহরণ ১: স্ট্রিং যোগ করা ও দেখানো
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("John");
strCol.Add("Tim");
strCol.Add("Gary");
strCol.Add("Katie");
strCol.Add("Andy");
strCol.Add("Amy");
strCol.Add("Scarlett");
strCol.Add("Jacob");
Console.WriteLine("Elements in StringCollection...");
foreach(Object ob in strCol) {
Console.WriteLine(ob);
}
Console.WriteLine("Count of elements = " + strCol.Count);
}
}
আউটপুট
Elements in StringCollection... John Tim Gary Katie Andy Amy Scarlett Jacob Count of elements = 8
উদাহরণ ২: কম এলিমেন্ট সহ উদাহরণ
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("One");
strCol.Add("Two");
strCol.Add("Three");
strCol.Add("Four");
strCol.Add("Five");
Console.WriteLine("Elements in StringCollection...");
foreach(Object ob in strCol) {
Console.WriteLine(ob);
}
}
}
আউটপুট
Elements in StringCollection... One Two Three Four Five
অন্যান্য দরকারী মেথড
AddRange(StringCollection)— অন্য StringCollection থেকে কপি করে।Insert(index, string)— নির্দিষ্ট ইনডেক্সে যোগ করে।Remove(string)— প্রথম ম্যাচ মুছে ফেলে।IndexOf(string)— প্রথম উপস্থিতির ইনডেক্স।