C# Program to Check if Value exists in Hashtable (2024)

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

The hashtable is an organized collection of key-value pairs wherein the keys are arranged as per the hash code of the key calculated using the hash function. While the keys should be non-null and unique in a hashtable, the values can be null and duplicate.

The elements in the hashtable are accessed using the keys. In C#, the class “Hashtable” represents the hashtable collection. This class provides various properties and methods using which we can perform operations and access data in the hashtable.

In this article, we will see how we can determine if a particular value is present in the hashtable.

How to Check if Value Exists in Hashtable?

To check if the value exists in a hashtable, we can make use of the “containsValue” method provided by the Hashtable class. This method returns a Boolean value indicating whether or not the specified value is present in the hashtable.

Let’s have a look at the method first before proceeding with programming examples.

ContainsValue Method

Syntax − public virtual bool ContainsValue (object value);

Description − used to find if the Hashtable contains a specified value.

Parameters − value(object) to be located in the hashtable. Can be a null value.

Return ValueBoolean: true=> hashtable contains an element with the specified value.

False=> hashtable does not contain an element with the specified value.

Namespace − System.Collections

Let’s now see few programming examples where we check if the specified value is present in the hashtable or not.

Example

The first program to check if the value is present in the hashtable is given below.

using System;using System.Collections;class Program { public static void Main(){ // Create a Hashtable Hashtable langCodes = new Hashtable(); // Add elements to the Hashtable langCodes.Add("C++", "CPlusPlus"); langCodes.Add("C#", "CSharp"); langCodes.Add("Java", "Java"); langCodes.Add("PL", "Perl"); // use ContainsValue method to check if the HashTable contains the //required Value or not. if (langCodes.ContainsValue("CSharp")) Console.WriteLine("langCodes hashtable contain the Value = CSharp"); else Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp"); }}

The above program declares a langCodes hashtable containing language code and language names as keys and values respectively. Next, we have an “if” construct that checks if the value “CSharp” is present in the hashtable. If it is present, it will display the message accordingly.

Output

The output of the program is shown below.

langCodes hashtable contain the Value = CSharp

Since the value = CSharp is present in the hashtable, the program gives the above message.

Now change the argument of the ContainsValue method to “C#” i.e. the key instead of value.

if (langCodes.ContainsValue("C#"))

Now execute the above program with this change.

Output

In this case, since the value “C#” is not present in the hashtable, the program will return the appropriate message. So we’ll get −

langCodes hashtable doesn't contain the Value = CSharp

Example

Now let’s look at the following example.

using System;using System.Collections;class Program { public static void Main() { // Create a Hashtable Hashtable NumberNames = new Hashtable(); // Add elements to the Hashtable NumberNames.Add(1, "One"); NumberNames.Add(3, "Three"); NumberNames.Add(5, "Five"); NumberNames.Add(7, "Seven"); // use ContainsValue method to check if the HashTable contains the //required Value or not. if (NumberNames.ContainsValue("Two")) Console.WriteLine("NumberNames hashtable contain the Value = Two"); else Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two"); if (NumberNames.ContainsValue("Five")) Console.WriteLine("NumberNames hashtable contain the Value = Five"); else Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five"); }}

This program has a “NumberNames” table with numbers as keys and their corresponding names as values. Here, we first check if the hashtable contains value = Two using the “containsKey()” method. Next, we check for the value = “Five” using the containsKey() method.

Output

The output for the program is shown below.

NumberNames hashtable doesn't contain the Value = TwoNumberNames hashtable contain the Value = Five

As we can see from the hashtable defined in the program, it does not contain value = Two but contains value = Five. So the program gives the messages appropriately.

Conclusion

Thus using the “containsKey()” method of the Hashtable class in C#, we can determine if an element with a specific value is present in the hashtable or not. Depending on whether the value is present or not, we can output the appropriate results, or in the case of a complex program, proceed with the appropriate code.

The method “containsKey()” thus becomes useful when we have to check for the presence of a specified value in the hashtable and then take the appropriate course of action.

Updated on: 22-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started

C# Program to Check if Value exists in Hashtable (31)

Advertisem*nts

'; adpushup.triggerAd(ad_id); });

C# Program to Check if Value exists in Hashtable (2024)

FAQs

C# Program to Check if Value exists in Hashtable? ›

ContainsValue(Object) Method is used to check whether the Hashtable contains a specific value or not. Syntax: public virtual bool ContainsValue(object value);

How do you check if a Hashtable contains a value? ›

Hashtable containsValue() Method in Java

containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Hashtable. It takes the Value as a parameter and returns True if that value is mapped by any of the keys in the table.

How to check if something exists in a Hashtable? ›

To check if the value exists in a hashtable, we can make use of the “containsValue” method provided by the Hashtable class. This method returns a Boolean value indicating whether or not the specified value is present in the hashtable.

How to check if a value exists in C#? ›

The LinkedList<T> generic class in the System. Collections. Generic namespace provides the Contains() method, which can be used to determine if a specified value exists in a linked list in C#.

How to find the value of a key in a Hashtable in C#? ›

To do this we use the hashtable collection method in C#, Contains() that returns true if the key is present in the hashtable or false if the key is not present. If Contains() method returns true, then we simply access the value of that particular key. string keyval = (string)Citytable[key];

How do you check if a hash contains a value? ›

Overview. A particular value can be checked to see if it exists in a certain hash by using the has_value?() method. This method returns true if such a value exists, otherwise false .

How to search for a value in a HashTable? ›

The user is prompted to enter a list of numbers, and the hash table is populated using the hash function. The contents of the hash table are printed. The user is asked to enter the number they want to search for. The hashFind function is called to search for the user-provided key in the hash table.

How to check if element exists in HashMap? ›

Java's HashMap uses the containsKey(Object key) method to check if a given key is present in the map. This method checks if the HashMap has a mapping for the supplied key when it is called. The function returns true if the key is present in the HashMap and returns false otherwise.

How to check if Hashtable is empty? ›

The isEmpty method of the Hashtable class is used to check if the specified Hashtable object is empty, i.e., it has no key-value entry present.

How to check if key-value exists in hash in Ruby? ›

The Hash#has_key? method is one of Ruby's built-in methods which can be used to check if a given Hash object contains a given key. It is widely used by Ruby contract developers and other software developers for creating complex data structures.

How to use if exists in C#? ›

Exists() Method in C# with Examples. File. Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false.

How to check if an object has a value or not in C#? ›

Syntax : public virtual bool ContainsValue (object value); Here, value is the value to locate in the SortedList object and it can be null. Return Value: This method returns True if the SortedList object contains an element with the specified value, otherwise it returns False.

How to check if something contains something in C#? ›

The . Contains() method determines whether a string includes a particular character or substring. It returns true if the character is included, otherwise the method returns false .

How do you check if a key is present in Hashtable? ›

containsKey() method is used to check whether a particular key is present in the Hashtable or not. It takes the key element as a parameter and returns True if that element is present in the table.

How do you check if a value is in a hash key in Perl? ›

In Perl, the exists() function checks whether a particular element exists or not in an array or a hash. If the requested element appears in the input array or hash, this function returns "1", else it returns "0".

How to get hash value in C#? ›

Getting the hash code of a string is simple in C#. We use the GetHashCode() method. A hash code is a uniquely identified numerical value. Note that strings that have the same value have the same hash code.

How do you check if a value is present for a key in HashMap? ›

Java's HashMap uses the containsKey(Object key) method to check if a given key is present in the map. This method checks if the HashMap has a mapping for the supplied key when it is called. The function returns true if the key is present in the HashMap and returns false otherwise.

How to check if key and value exists in hash Ruby? ›

We can check if a particular hash contains a particular key by using the method has_key?(key) . It returns true or false depending on whether the key exists in the hash or not.

How do I know if HashMap contains the same value? ›

equals() to check if two HashMaps have the same entries. The way that Map. equals() works is by comparing keys and values using the Object. equals() method.

Top Articles
6 Best Scalping Forex Brokers for September,2024
The Ultimate Crypto Wallet for DeFi, Web3 Apps, and NFTs | MetaMask
Ffxiv Act Plugin
Knoxville Tennessee White Pages
Moon Stone Pokemon Heart Gold
Readyset Ochsner.org
Apex Rank Leaderboard
Unraveling The Mystery: Does Breckie Hill Have A Boyfriend?
Elden Ring Dex/Int Build
Skip The Games Norfolk Virginia
My.doculivery.com/Crowncork
Elizabethtown Mesothelioma Legal Question
Missing 2023 Showtimes Near Landmark Cinemas Peoria
Gino Jennings Live Stream Today
Munich residents spend the most online for food
Tamilrockers Movies 2023 Download
Katherine Croan Ewald
Diamond Piers Menards
Site : Storagealamogordo.com Easy Call
Is Windbound Multiplayer
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Integer Division Matlab
Horn Rank
Mals Crazy Crab
Cognitive Science Cornell
Cornedbeefapproved
Craigslist Fort Smith Ar Personals
Jazz Total Detox Reviews 2022
The Clapping Song Lyrics by Belle Stars
Poe T4 Aisling
R/Sandiego
Pfcu Chestnut Street
Max 80 Orl
Beaver Saddle Ark
How to Get Into UCLA: Admissions Stats + Tips
Log in or sign up to view
Today's Final Jeopardy Clue
Finland’s Satanic Warmaster’s Werwolf Discusses His Projects
The Minneapolis Journal from Minneapolis, Minnesota
Saybyebugs At Walmart
Gvod 6014
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Candise Yang Acupuncture
Tlc Africa Deaths 2021
Youravon Com Mi Cuenta
Nope 123Movies Full
Kushfly Promo Code
Diario Las Americas Rentas Hialeah
Kidcheck Login
Marion City Wide Garage Sale 2023
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 6221

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.