Get dictionary value by key
Solution 1
It's as simple as this:
String xmlfile = Data_Array["XML_File"];
Note that if the dictionary doesn't have a key that equals "XML_File"
, that code will throw an exception. If you want to check first, you can use TryGetValue like this:
string xmlfile;
if (!Data_Array.TryGetValue("XML_File", out xmlfile)) {
// the key isn't in the dictionary.
return; // or whatever you want to do
}
// xmlfile is now equal to the value
Solution 2
Just use the key name on the dictionary. C# has this:
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("UserID", "test");
string userIDFromDictionaryByKey = dict["UserID"];
If you look at the tip suggestion:
Solution 3
That is not how the TryGetValue
works. It returns true
or false
based on whether the key is found or not, and sets its out
parameter to the corresponding value if the key is there.
If you want to check if the key is there or not and do something when it's missing, you need something like this:
bool hasValue = Data_Array.TryGetValue("XML_File", out value);
if (hasValue) {
xmlfile = value;
} else {
// do something when the value is not there
}
Solution 4
Dictionary<String, String> d = new Dictionary<String, String>();
d.Add("1", "Mahadev");
d.Add("2", "Mahesh");
Console.WriteLine(d["1"]); // It will print Value of key '1'
Solution 5
static void XML_Array(Dictionary<string, string> Data_Array)
{
String value;
if(Data_Array.TryGetValue("XML_File", out value))
{
// ... Do something here with value ...
}
}
Related videos on Youtube
Matei Zoc
Updated on July 18, 2022Comments
-
Matei Zoc almost 2 years
How can I get the dictionary value by a key on a function?
My function code (and the command I try doesn't work):
static void XML_Array(Dictionary<string, string> Data_Array) { String xmlfile = Data_Array.TryGetValue("XML_File", out value); }
My button code:
private void button2_Click(object sender, EventArgs e) { Dictionary<string, string> Data_Array = new Dictionary<string, string>(); Data_Array.Add("XML_File", "Settings.xml"); XML_Array(Data_Array); }
I want on the
XML_Array
function the variable to be:string xmlfile = "Settings.xml":
-
Peter Mortensen over 2 yearsRe "the command I try doesn't work": What happens? What are the symptoms?
-
-
Ladislav Ondris almost 6 yearsIt throws an exception if the key does not exist. That is why other people's answers suggest that you should use TryGetValue.
-
FrenkyB almost 6 yearsI don't think this is the reason that others are suggesting TryGetValue. My solution is simplification, which I was not aware of. When I've found it out, I've pasted it here. And it seems that a lot of others didn't know about that also. Otherwise, they could also paste this answer and add that throws exception if key doesn't exist. Anyway, thanks for warning.
-
Ortund over 3 yearsFor some reason I always forget this with Dictionaries... Like it doesn't seem like it should be this straightforward.
-
Koenman about 3 yearsStill a good solution when you know that values will always exiist. If not, it would still recommend the answers that suggest using TryGetValue().
-
BDL about 3 yearsThis would search for the key twice in the dictionary. Why not use the out parameter of TryGetValue instead of searching a second time? Even with your code: Why even use TryGetValue instead of ContainsKey if you want to go that route?
-
Peter Mortensen over 2 yearsHow does that answer the question? "How can I get the dictionary value by a key" An explanation would be in order. E.g., what is the idea/gist? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
-
Peter Mortensen over 2 yearsIs this the answer to a different question?
-
Peter Mortensen over 2 yearsAn explanation would be in order. E.g., what is the idea/gist? What was changed? What was the fix? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
-
Peter Mortensen over 2 yearsThere isn't anyone here by the name "dasblinkenlight". What answer does it refer to?
-
Peter Mortensen over 2 yearsAn explanation would be in order. E.g., what is the idea/gist? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
-
Peter Mortensen over 2 yearsYou have got some explaining to do. (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
-
The incredible Jan about 2 yearsI have no use for hasValue : if (Data_Array.TryGetValue("XML_File", out value)) { xmlfile = value; } else { // do something when the value is not there }