ContainsValue in C#


Use the ContainsValue() method in C# to search for a value in a Dictionary.

Create a Dictionary and add elements −

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("keyboard", "One");
d.Add("mouse", "Two");

Now, use the ContainsValue() method to find a particular value −

d.ContainsValue("Two");

The following is the complete code −

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
class Program {
   static void Main() {
      Dictionary<string, string> d = new Dictionary<string, string>();
      d.Add("keyboard", "One");
      d.Add("mouse", "Two");
      // search for a value
      Console.WriteLine(d.ContainsValue("Two"));
   }
}

Output

True

Updated on: 22-Jun-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements