HashTable Tutorial In C#

CK CK 3788 
Created at
Updated at  
1,789 0 0

Adding objects to a Hashtable is just like adding to a collection since they both inherit from the same parent.
 

Hashtable h = new Hashtable();

h.Add("Key1", "Value1");
h.Add("Key2", "Value2");
h.Add("Key3", "Value3");
h.Add("Key4", "Value4");
h.Add("Key5", "Value5");

Determining if an Object is in the Hashtable
You can search the Key and the Value lists in a Hashtable for a specific entry:

Here is an example of checking the Key list:

Console.WriteLine(h.ContainsKey("Key5"))

Here is an example of checking the Value list:

Console.WriteLine(h.ContainsValue("Value2"));

Reading an Object in the Hashtable

To read an object from the Hashtable just use the Item method like so:

Console.WriteLine(h.Item("Key3"));

Enumerating the Objects in the Hashtable
One thing to remember is that the objects may not come out of the HashTable in the same order as you put them in. This is due to the hashing algorithm that keeps things running fast. 

IDictionaryEnumerator en = h.GetEnumerator();

while (en.MoveNext())
{
   Console.WriteLine(en.Key   " : "   en.Value);
}
Tags ContainsKey ContainsValue GetEnumerator Hash Hashtable MoveNext Facebook X
Comments 0
Similar posts
  1. 32bits simple hash function in PHP
    40
  2. Bob Jenkins' One-At-A-Time hashing algorithm in PHP
    54
  1. Adding Custom Paper Sizes To Named Printers
    649
  2. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  3. A Customizable Printing Text Class
    433
  4. Simplified .NET Printing In C#
    693
  5. How To Print Text In C#
    455
  6. C# Lists
    1,152
  7. Simple Example of IF/THEN using C#
    1,266
  8. Fast and Good Keyboard/Mouse Test without the message handler
    982
  9. Simple CGI Programming in C
    545
  10. C++ CGI Example working on Apache
    486
  11. Simple C# CGI working on Apache
    481
  12. How to send binary data through C# CGI app?
    996
  13. Interacting With TinyPic From C#
    1,307
  14. Creating A Watched Folder With Assigned Events
    1,662
  15. Calling Your Main Thread From A Worker Thread In C#
    547
  16. Stack Tutorial In C#
    669
  17. Queue Tutorial In C#
    1,739
  18. Create PDF Files On the Fly in C#
    1,004
  19. How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport
    1,164
  20. Send Email Using C#
    1,378
  21. How To Read And Write A Cookie
    363
  22. Using C# With Cookies
    626
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,039
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263