
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to get hash code for the specified key of a Hashtable in C#?
To get hash code for the specified key of a Hashtable, the code is as follows −
Example
using System; using System.Collections; public class HashCode : Hashtable { public static void Main(string[] args) { HashCode hash = new HashCode(); hash.Add("A", "Jacob"); hash.Add("B", "Mark"); hash.Add("C", "Tom"); hash.Add("D", "Nathan"); hash.Add("E", "Tim"); hash.Add("F", "John"); hash.Add("G", "Gary"); Console.WriteLine("Key and Value pairs..."); foreach(DictionaryEntry entry in hash) { Console.WriteLine("{0} and {1} ", entry.Key, entry.Value); } Console.Write("HashCode for key D =" + (hash.GetHash("D")) ); } }
Output
This will produce the following output −
Key and Value pairs... G and Gary A and Jacob B and Mark C and Tom D and Nathan E and Tim F and John HashCode for key D =-842352676
Example
Let us see another example −
using System; using System.Collections; public class HashCode : Hashtable { public static void Main(string[] args) { HashCode hash = new HashCode(); hash.Add('1', "One"); hash.Add('2', "Two"); hash.Add('3', "Three"); hash.Add('4', "Four"); Console.WriteLine("Key and Value pairs..."); foreach(DictionaryEntry entry in hash) { Console.WriteLine("{0} and {1} ", entry.Key, entry.Value); } Console.WriteLine("HashCode for key 1 = " + (hash.GetHash('1'))); Console.WriteLine("HashCode for key 2 = " + (hash.GetHash('2'))); Console.WriteLine("HashCode for key 3 = " + (hash.GetHash('3'))); Console.WriteLine("HashCode for key 4 = " + (hash.GetHash('4'))); } }
Output
This will produce the following output −
Key and Value pairs... 3 and Three 2 and Two 4 and Four 1 and One HashCode for key 1 = 3211313 HashCode for key 2 = 3276850 HashCode for key 3 = 3342387 HashCode for key 4 = 3407924
- Related Articles
- How to Get Value from HashTable Collection in C# using Specified Key
- Get Hash Code for Integer in Java
- Get or Set the value associated with specified key in Hashtable in C#
- Get the hash code for this instance in C#
- Get the hash code for the current Int64 instance in C#
- Get the hash code for the current Decimal instance in C#
- Remove the element with the specified key from the Hashtable in C#
- Obtain the hash code for a string in Java
- Format Specifier for Hash Code in Java
- C# Program to Get Key based on Value in Hashtable Collection
- How to Check if a Key Exists in the Hashtable in C#?
- Checking for Key/Value Existence in Perl Hash
- Golang program to get key based on value from the hash collection
- How to create Android Facebook Key Hash?
- C# Program to find a key in a Hashtable

Advertisements