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.
- using System.Collections.Specialized;
- using System.Collections;
We use System.Collections namespace if we need to retrieve the data using ICollection.
Code
- OrderedDictionary person = new OrderedDictionary();
- person.Add("Name","Salman");
- person.Add("Address","Pakistan");
Retrieve the data using LINQ Element Query Operator.
- for(int i=0; i<person.Count; i++)
- {
- var item = person.ElementAt(i);
- Console.WriteLine("Key: " + item.Key);
- Console.WriteLine("Value: " + item.Value);
- }
Retrieve the data usnig ICollection.
- ICollection keyCOllection = person.Keys;
- ICollection valueCollection = person.Values;
-
- String[] myKeys = new String[person.Count];
- String[] myValues = new String[person.Count];
-
- keyCOllection.CopyTo(myKeys, 0);
- valueCollection.CopyTo(myValues, 0);
-
- for(int i=0; i<person.Count; i++)
- {
- Console.WriteLine("Key: " + myKeys[i]);
- Console.WriteLine("Value: " + myValues[i]);
- }
I hope you understand.