Find a Key
The ContainsKey method checks if a key already exists in the dictionary. The following code snippet checks if a key already exits and if not, adds it.
- if (!AuthorList.ContainsKey("Mahesh Chand"))
- {
- AuthorList["Mahesh Chand"] = 20;
- }
The ContainsValue method checks if a value already exists in
the dictionary. The following code snippet checks if a value already
exits.
Sample
- if (!AuthorList.ContainsValue(9))
- {
- Console.WriteLine("Item found");
- }
Here is the complete sample code showing how to use these
methods.
- // Create a dictionary with string key and Int16 value pair
- Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();
- AuthorList.Add("Mahesh Chand", 35);
- AuthorList.Add("Mike Gold", 25);
- AuthorList.Add("Praveen Kumar", 29);
- AuthorList.Add("Raj Beniwal", 21);
- AuthorList.Add("Dinesh Beniwal", 84);
- // Count
- Console.WriteLine("Count: {0}", AuthorList.Count);
- // Set Item value
- AuthorList["Neel Beniwal"] = 9;
- if (!AuthorList.ContainsKey("Mahesh Chand"))
- {
- AuthorList["Mahesh Chand"] = 20;
- }
- if (!AuthorList.ContainsValue(9))
- {
- Console.WriteLine("Item found");
- }
- // Read all items
- Console.WriteLine("Authors all items:");
- foreach (KeyValuePair<string, Int16> author in AuthorList)
- {
- Console.WriteLine("Key: {0}, Value: {1}",
- author.Key, author.Value);
- }
Read more >> Using C# Dictionary Class

Sunil jadhavPosted Feb 13, 2019, 4:53 AM
Private Dictionary<int, string> m_CodesnPackDtlsID = new Dictionary<int, string>();var key = m_CodesnPackDtlsID.FirstOrDefault(x => x.Value == value).Key;
Sunil jadhavPosted Feb 13, 2019, 4:52 AM
Using linq expression we can find out key from dictionary by passing its value.
Sunil jadhavPosted Feb 13, 2019, 4:51 AM
Public int GetUIDsKey(string value) { if (m_CodesnPackDtlsID.Count > 0) { Trace.TraceInformation("##GetUIDsKey Count : {0},{1}", DateTime.Now.ToString(), m_CodesnPackDtlsID.Count); var key = m_CodesnPackDtlsID.FirstOrDefault(x => x.Value == value).Key; return key; } return 0; }
Rajesh KannaneditedPosted May 6, 2013, 9:02 AMEdited May 6, 2013, 9:06 AM
I need extract keystrokes latencies(when a particular key was pressed and released) from the user(if they typing). Please Help me..................