Link to home
Start Free TrialLog in
Avatar of izloch2
izloch2

asked on

get fist element from dictionary in C#

Hi! Is there a simple way to return the first element from Dictionary. I'm trying to do it the following way but it doesn't work:

            Dictionary<string, bool> temp = new Dictionary<string,bool>();
            temp.Add("Z", false);
            temp.Add("T", false);
            temp.Add("A", false);
            temp.Add("D", false);

            string t = temp.GetEnumerator().Current.Key;

I need to get t="Z" from the dictionary, but i get t equal to null

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The order in which keys and values are returned is not the necessarily same order used for the inserts.

Dictionary<string, bool>.KeyCollection keys = temp.Keys;

Now you can search the key collection for "Z".

Jim