C# में StringCollection वर्ग की Count प्रॉपर्टी का उपयोग करके संग्रह में मौजूद स्ट्रिंग्स की कुल संख्या प्राप्त की जा सकती है। यह प्रॉपर्टी केवल-पढ़ने योग्य है और संग्रह में तत्वों की वर्तमान संख्या को पूर्णांक के रूप में लौटाती है।
उदाहरण 1: सरणी से StringCollection बनाकर Count प्राप्त करना
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 strCol = new StringCollection();
String[] strArr = new String[] { "10", "20", "30", "40", "50" };
Console.WriteLine("StringCollection तत्व...");
foreach (string str in strArr) {
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("StringCollection में स्ट्रिंग्स की संख्या = " + strCol.Count);
}
}
आउटपुट
StringCollection तत्व... 10 20 30 40 50 StringCollection में स्ट्रिंग्स की संख्या = 5
मुख्य बिंदु
Countप्रॉपर्टी केवल-पढ़ने योग्य हैAddRange()से सरणी के सभी तत्व एक साथ जोड़ सकते हैंAdd()से एक-एक करके तत्व जोड़ सकते हैं- सूचकांकर
[index]से विशिष्ट स्थिति पर तत्व प्राप्त कर सकते हैं - आधुनिक कोड में
List<string>का उपयोग करने की अनुशंसा की जाती है