C#-এ StringCollection.Insert(int index, string value) মেথড একটি নির্দিষ্ট ইনডেক্সে স্ট্রিং ইনসার্ট করে। এটি System.Collections.Specialized নামস্পেসে থাকে এবং স্ট্রিং-ওনली কালেকশন।
উদাহরণ ১: StringCollection-এ একট ইনসার্ট
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Accessories");
strCol.Add("Books");
strCol.Add("Electronics");
Console.WriteLine("StringCollection এলিমেন্টসমূহ...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
Console.WriteLine("StringCollection এলিমেন্টসমূহ (আপডেটেড)...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
}
}
আউটপুট
StringCollection এলিমেন্টসমূহ... Accessories Books Electronics StringCollection এলিমেন্টসমূহ (আপডেটেড)... Accessories Books Headphone Electronics
উদাহরণ ২: একাধিক ইনসার্ট
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Laptop");
strCol.Add("Desktop");
strCol.Add("Tablet");
strCol.Add("Speakers");
Console.WriteLine("StringCollection এলিমেন্টসমূহ...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(2, "Headphone");
strCol.Insert(3, "Alienware");
Console.WriteLine("StringCollection এলিমেন্টসমূহ (আপডেটেড)...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
strCol.Insert(4, "E-Book Reader");
strCol.Insert(5, "Monitor");
strCol.Insert(6, "HDD");
strCol.Insert(7, "SSD");
Console.WriteLine("StringCollection এলিমেন্টসমূহ (আপডেটেড)...");
foreach (string res in strCol) {
Console.WriteLine(res);
}
}
}
আউটপুট
StringCollection এলিমেন্টসমূহ... Laptop Desktop Tablet Speakers StringCollection এলিমেন্টসমূহ (আপডেটেড)... Laptop Desktop Headphone Alienware Tablet Speakers StringCollection এলিমেন্টসমূহ (আপডেটেড)... Laptop Desktop Headphone Alienware E-Book Reader Monitor HDD SSD Tablet Speakers
ব্যাখ্যা: Insert মেথড নির্দিষ্ট ইনডেক্সে মান যোগ করে এবং বাকি এলিমেন্টগুলো ডানদিকে শিফট করে। ইনডেক্স ০-ভিত্তিক। StringCollection পুরানো নন-জেনেরিক কালেকশন, আধুনিক কোডে List<string> ব্যবহার করা ভালো।