a certain value exists in a dictionary in C#

In C#, you can use the ContainsValue method to determine if a dictionary contains a specific value. Here is an example code:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary<int, string> dictionary = new Dictionary<int, string>();
        dictionary.Add(1, "apple");
        dictionary.Add(2, "banana");
        dictionary.Add(3, "orange");

        string searchValue = "banana";

        bool containsValue = dictionary.ContainsValue(searchValue);

        if (containsValue)
        {
            Console.WriteLine($"The dictionary contains the value '{searchValue}'.");
        }
        else
        {
            Console.WriteLine($"The dictionary does not contain the value '{searchValue}'.");
        }
    }
}

In the example above, we first create a dictionary containing key-value pairs, then use the ContainsValue method to check if the dictionary contains an entry with the value “banana”. Finally, we output the corresponding message based on the result of the check.

 

 

More tutorials

How to add elements to a C# dictionary(Opens in a new browser tab)

How to retrieve a value from a C# dictionary using a key?(Opens in a new browser tab)

How can I include new entries in a Python dictionary?(Opens in a new browser tab)

How do you call AutoResetEvent in C#?(Opens in a new browser tab)

Sort the Java Collections using the sort() method.(Opens in a new browser tab)

Leave a Reply 0

Your email address will not be published. Required fields are marked *