using System.Collections.Generic; // ... DictionarymyDict = new Dictionary () { { "Alice", 25 }, { "Bob", 30 }, { "Charlie", 35 } }; bool hasKey = myDict.ContainsKey("Bob"); // true bool hasValue = myDict.ContainsValue(25); // true if (hasKey && hasValue) { Console.WriteLine("Bob's age is 30!"); // Output: Bob's age is 30! }
using System.Linq; // ... DictionaryThis example creates a dictionary of string keys and string values. It then uses the LINQ library to check if the dictionary contains the value "red" or "orange". The first condition is true, so the program outputs "We have an apple!". The second condition is false, so the program outputs "We don't have an orange.". Package Library: The System.Collections Dictionary Any method is part of the base class library of .NET Framework, .NET Core, and .NET Standard.myDict = new Dictionary () { { "apple", "red" }, { "banana", "yellow" }, { "kiwi", "green" } }; bool hasRed = myDict.Any(pair => pair.Value == "red"); // true bool hasOrange = myDict.Any(pair => pair.Value == "orange"); // false if (hasRed) { Console.WriteLine("We have an apple!"); // Output: We have an apple! } if (!hasOrange) { Console.WriteLine("We don't have an orange."); // Output: We don't have an orange. }