Collectionsසහ Genericsවස්තු සමූහය හැසිරවීමට ප්රයෝජනවත් වේ. .NET, සෑම එකතු වස්තූන් ලෙස එම අතුරුමුහුණත යටතේ එන IEnumerableඅනෙක් අතට ඇති, ArrayList(Index-Value))සහ HashTable(Key-Value). .NET රාමුව 2.0 පසු, ArrayListසහ HashTableඇති කරන ලදි Listසහ Dictionary. දැන්, Arraylist&HashTable වර්තමානයේ ව්යාපෘති වල තවදුරටත් භාවිතා නොවේ.
HashTable& Dictionary, අතර වෙනස වෙත පැමිණීම Dictionaryසාමාන්ය Hastableනොවන අතර සාමාන්ය වේ. අපට ඕනෑම වර්ගයක වස්තුවක් එක් කළ හැකිය HashTable, නමුත් ලබා ගැනීමේදී එය අවශ්ය වර්ගයට දැමිය යුතුය. එබැවින්, එය ආරක්ෂිත ටයිප් නොවේ. නමුත්dictionary , එය ප්රකාශ කරන අතරම අපට යතුර සහ අගය වර්ගය නියම කළ හැකිය, එබැවින් නැවත ලබා ගැනීමේදී වාත්තු කිරීමේ අවශ්යතාවයක් නොමැත.
උදාහරණයක් බලමු:
හැෂ්ටේබල්
class HashTableProgram
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
foreach (DictionaryEntry de in ht)
{
int Key = (int)de.Key; //Casting
string value = de.Value.ToString(); //Casting
Console.WriteLine(Key + " " + value);
}
}
}
ශබ්ද කෝෂය,
class DictionaryProgram
{
static void Main(string[] args)
{
Dictionary<int, string> dt = new Dictionary<int, string>();
dt.Add(1, "One");
dt.Add(2, "Two");
dt.Add(3, "Three");
foreach (KeyValuePair<int, String> kv in dt)
{
Console.WriteLine(kv.Key + " " + kv.Value);
}
}
}