check to see if a dictionary contains a value in csharp

To check if a dictionary contains a value in C#, you can use the ContainsValue() method. This method returns true if the value is found in the dictionary, otherwise it returns false.

Here's an example:

main.cs
// create a dictionary
Dictionary<string, int> dict = new Dictionary<string, int>();

// add some values to the dictionary
dict.Add("apple", 1);
dict.Add("banana", 2);
dict.Add("orange", 3);

// check if the dictionary contains a value
if (dict.ContainsValue(2))
{
    Console.WriteLine("The dictionary contains the value 2.");
}
else
{
    Console.WriteLine("The dictionary does not contain the value 2.");
}
410 chars
18 lines

In this example, we create a dictionary of type string and int and add some key-value pairs to it. We then use the ContainsValue() method to check if the dictionary contains the value 2. If it does, we print a message to the console. If not, we print a different message.

gistlibby LogSnag