TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# ContainsKey Method (Key Exists in Dictionary)

Use the ContainsKey method on the Dictionary to test for key existence.
ContainsKey. This is a Dictionary method. It computes the hash code for its argument. It then checks the internal structures in the Dictionary to see if that key exists.ContainsValueTryGetValue
Method notes. ContainsKey is extremely fast. It does no linear searching. It uses a hash code to estimate where the data is stored.GetHashCode
An example. You will find the ContainsKey method on a Dictionary. In Visual Studio, try typing the variable name and pressing period, and then scrolling to ContainsKey.Dictionary

Info: The program populates the Dictionary with 2 key-value pairs. ContainsKey is invoked twice.

Argument: The ContainsKey method receives one parameter. This is the key we wish to check for.

Returns: ContainsKey returns a Boolean value that indicates whether the key was found in the Dictionary or not.

Bool

And: You can test ContainsKey in an if-statement. It will not throw exceptions on a key that is not found.

IfTrue, False
C# program that uses ContainsKey using System; using System.Collections.Generic; class Program { static void Main() { // Create Dictionary with two key value pairs. var dictionary = new Dictionary<string, int>() { {"mac", 1000}, {"windows", 500} }; // Use ContainsKey method. if (dictionary.ContainsKey("mac") == true) { Console.WriteLine(dictionary["mac"]); // <-- Is executed } // Use ContainsKey method on another string. if (dictionary.ContainsKey("acorn")) { Console.WriteLine(false); // <-- Not hit } } } Output 1000
Internals. The .NET Framework implements the ContainsKey method. Dictionary has a FindEntry() method which loops over the entries in the Dictionary that are pointed to by the buckets array.

And: When it finds a matching hash code, it compares the key values and then returns the actual value.

Returns: The ContainsKey method discards some of the values it finds and simply returns a Boolean.

Therefore: Using TryGetValue can be used to perform these operations at one time, improving runtime speed.

Benchmark. A benchmark of ContainsKey compared to ContainsValue shows that ContainsKey is many times faster (the exact amount depends on the Dictionary size). This is important to know.
A summary. ContainsKey checks for a key's existence. Dictionary implements a lightning-fast lookup table. But it will throw exceptions if you try to access an entry when no key exists.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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