[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


get fist element from dictionary in C#

Posted on 2007-10-15
Medium Priority
Last Modified: 2012-05-05
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
Question by:izloch2
LVL 96

Accepted Solution

Bob Learned earned 1500 total points
ID: 20079462
Try this:

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

            string t = "";
            foreach (string key in temp.Keys)
                t = key;

LVL 22

Expert Comment

ID: 20079491
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".


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question