ContainsValue in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 05-Sep-2018 07:42:06
Advertisements