उदाहरण 1: सरणी से StringCollection बनाना
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" };
Console.WriteLine("StringCollection तत्व...");
foreach (string str in strArr) {
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("5वें इंडेक्स पर तत्व = " + strCol[5]);
Console.WriteLine("StringCollection में स्ट्रिंग्स की संख्या = " + strCol.Count);
}
}
आउटपुट
StringCollection तत्व...
A
B
C
D
E
F
G
H
5वें इंडेक्स पर तत्व = F
StringCollection में स्ट्रिंग्स की संख्या = 8
उदाहरण 2: StringCollection में डुप्लिकेट हटाना
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "100", "400", "500" };
Console.WriteLine("सरणी तत्व...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("कुल तत्वों की संख्या = " + stringCol.Count);
stringCol.Remove("100");
Console.WriteLine("अब तत्वों की कुल संख्या = " + stringCol.Count);
}
}
आउटपुट
सरणी तत्व...
100
200
100
400
500
कुल तत्वों की संख्या = 5
अब तत्वों की कुल संख्या = 4