TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ContainsValue Method

This C# example program uses the ContainsValue method on the Dictionary type.

ContainsValue searches for a value in a Dictionary.

Sometimes you cannot access a Dictionary only by looking up keys and need to search for a specific value. This method on the Dictionary type provides this functionality.

Example. The ContainsValue method receives one parameter of the type specified as the value type in the Dictionary. When you declare the Dictionary, you specify two type parameters. The second, TValue parameter is the type used in ContainsValue.

Here: This example shows how the ContainsValue searches all entries in the Dictionary for a match and returns a bool.

Bool

C# program that uses ContainsValue

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	//
	// This dictionary contains stock symbols and market caps.
	// ... Values are in billions of USD.
	//
	var caps = new Dictionary<string, decimal>();
	caps.Add("msft", 215);
	caps.Add("xon", 348);
	caps.Add("goog", 132);
	caps.Add("wmt", 188);

	//
	// See if there are stocks with these capitalizations.
	//
	bool flag1 = caps.ContainsValue(348);
	bool flag2 = caps.ContainsValue(1000);
	bool flag3 = caps.ContainsValue(188);

	//
	// The first call matches "xon";
	// The second call returns false;
	// The third call matches "wmt".
	//
	Console.WriteLine(flag1);
	Console.WriteLine(flag2);
	Console.WriteLine(flag3);
    }
}

Output

True
False
True

In this example, a new Dictionary containing keys of type string and values of type decimal is instantiated. It is populated with four stock symbols and their market valuations in billions of USD.

Note: The companies are Microsoft, Exxon, Google and Wal-Mart. This information is not important. I am sharing it anyways.

The three bool variables flag1, flag2 and flag3 are assigned the results of ContainsValue on various parameters. In Code Complete using the variable name of "flag" is discouraged because it does not describe the code's intent.

Also: In Refactoring, Martin Fowler suggests eliminating temporary variables and using the method calls themselves as the expressions.

Discussion. ContainsKey will compute a hash code of the key and use that to locate the value for that key in near-constant time. ContainsValue will loop through all the entries in the Dictionary. It checks the value of each element.

And: For this reason, using ContainsValue is far slower in most cases than ContainsKey.

ContainsKey

Summary. We saw how to use the ContainsValue method on the Dictionary generic class in the C# programming language targeting the .NET Framework. We saw an example of how this method can be used to search the values in a Dictionary.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf