C# में जेनेरिक संग्रह में <List>, <SortedList>, आदि शामिल हैं।
List
List<T> एक जेनेरिक संग्रह है और ArrayList एक नॉन-जेनेरिक संग्रह है।
आइए एक उदाहरण देखें। यहां, लिस्ट में छह तत्व हैं:
उदाहरण
using System; using System.Collections.Generic; class Program { static void Main() { // संग्रह प्रारंभ करना List<string> myList = new List<string>() { "one", "two", "three", "four", "five", "six" }; Console.WriteLine(myList.Count); } }आउटपुट
6
SortedList
एक सॉर्टेड लिस्ट एक सरणी और एक हैश टेबल का संयोजन है। इसमें आइटम्स की एक सूची होती है जिसे कुंजी या इंडेक्स का उपयोग करके एक्सेस किया जा सकता है।
आइए एक उदाहरण देखें। यहां, SortedList में चार तत्व हैं:
उदाहरण
using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Tim"); sl.Add("002", "Steve"); sl.Add("003", "Bill"); sl.Add("004", "Tom"); if (sl.ContainsValue("Bill")) { Console.WriteLine("This name is already in the list"); } else { sl.Add("005", "James"); } ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + sl[k]); } } } }आउटपुट
This name is already in the list 001: Tim 002: Steve 003: Bill 004: Tom