Access Data From Ordered Dictionary

Ordered Dictionary

When we need to represent the data as key/ pair, the best option is Ordered Dictionary. We later access the data using key or index.
 
Namespace

We need to use two namespaces.
  1. using System.Collections.Specialized;  
  2. using System.Collections;  
We use System.Collections namespace if we need to retrieve the data using ICollection.
 
Code
  1. OrderedDictionary person = new OrderedDictionary();  
  2. person.Add("Name","Salman");  
  3. person.Add("Address","Pakistan");  
Retrieve the data using LINQ Element Query Operator.
  1. for(int i=0; i<person.Count; i++)  
  2. {  
  3. var item = person.ElementAt(i);  
  4. Console.WriteLine("Key: " + item.Key);  
  5. Console.WriteLine("Value: " + item.Value);  
  6. }  
Retrieve the data usnig ICollection.
  1. ICollection keyCOllection = person.Keys;  
  2.             ICollection valueCollection = person.Values;  
  3.   
  4.             String[] myKeys = new String[person.Count];  
  5.             String[] myValues = new String[person.Count];  
  6.   
  7.             keyCOllection.CopyTo(myKeys, 0);  
  8.             valueCollection.CopyTo(myValues, 0);  
  9.   
  10. for(int i=0; i<person.Count; i++)  
  11. {  
  12. Console.WriteLine("Key: " + myKeys[i]);  
  13. Console.WriteLine("Value: " + myValues[i]);  
  14. }  
I hope you understand.
Ebook Download
View all
Learn
View all