. Advertisement .
..3..
. Advertisement .
..4..
Little did you know, digging into the C# dictionary means you will have a lot of side aspects to deal with. The C# dictionary get value by key is one of those problems we wish to mention.
Yet, worry no longer! This tutorial will be your right hand to run through any related trouble in the front sight. Read on for further details.
C# Dictionary Get Value By Key – How To
There are two approaches in total to acquire values using keys in the C# dictionary. Let’s check out what they are!
#1. The [] Feature
Before getting into this method, you must first inform yourself of the definition of the dictionary<T1,T2> class. This is one of the most significant characteristics in C#.
When you wish to state a dictionary, such a feature can be of great use to achieve that data structure under key-value pairs’ form no matter what.
As such, below is one example of how we should employ the [] method to get this kind of value utilizing the appropriate key.
Running the code:
using System;
using System.Collections.Generic;
namespace get_dictionary_value
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> mydictionary = new Dictionary<string, string>();
mydictionary.Add("Key 1", "Value 1");
mydictionary.Add("Key 2", "Value 2");
mydictionary.Add("Key 3", "Value 3");
Console.WriteLine(mydictionary["Key 3"]);
}
}
}
Output:
Value 3
All the above procedure could be summarized in merely 2 steps as follows:
- Generate a dictionary (mydictionary as in the code) by adopting the Dictionary<string, string>.
- Employing the [] function to retrieve the key 3’s value.
And that’s how you can succeed in getting value by key in C# dictionary without any considerable difficulty. If there is any downside, perhaps it is just about the risk of it raising an exception as the key is null or undefined.
#2. The TryGetKey() Method
In C#, the TryGetKey() method determines whether or not a key is present in a dictionary. Speaking of which, a boolean value has a very high likelihood to get returned by the TryGetKey() method.
The function changes the value of the out argument to the value of the key in the dictionary and sends back true if the key is present in the dictionary.
Otherwise, the method will return false if the key is absent from the dictionary.
Once the key is absent from the dictionary, the [] method’s exception will be under the controllation of the TryGetKey() function.
Belows is steps you should employ with this method to retrieve a dictionary’s value by key.
using System;
using System.Collections.Generic;
namespace get_dictionary_value
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, string> mydictionary = new Dictionary<string, string>();
mydictionary.Add("Key 1", "Value 1");
mydictionary.Add("Key 2", "Value 2");
mydictionary.Add("Key 3", "Value 3");
string value;
bool hasValue = mydictionary.TryGetValue("Key 3", out value);
if (hasValue)
{
Console.WriteLine(value);
}
else
{
Console.WriteLine("Key not present");
}
}
}
}
Output:
Value 3
Conclusion
Above is our in depth instruction regarding the C# dictionary get value by key. Hopefully, this article will be of benefit to you somehow. See then!
Read more:
Leave a comment