Introduction and usage of HashTable in C #

Source: Internet
Author: User

I. Introduction

Terms: A Hash table is a data structure that is directly accessed based on the Key value. That is to say, It maps the key value to a location in the table to access records to speed up the search. This ing function is called a hash function, and the array storing records is called a hash function.

Advantages of HashTable tables: HashTable is a container provided by the System. Collections namespace. The key/value in HashTable is of the object type, so HashTable can support any type of key/value pairs.

The advantage of HashTable lies in its indexing method, which is very fast.

Ii. Simple operations on Hash Tables

Add a keyvalue key-value pair in the hash table: HashtableObject. Add (key, value );

Remove a keyvalue key-value pair in the hash table: HashtableObject. Remove (key );

Remove all elements from the hash table: HashtableObject. Clear ();

Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );

The following console contains all the preceding operations:

123456789101112131415161718192021222324252627282930313233343536373839
Using System; using System. collections; // file when using Hashtable, you must introduce this namespace class hashtable {public static void Main () {Hashtable ht = new Hashtable (); // file to create a Hashtable instance ht. add (E, e); // Add the key-Value Pair ht. add (A, a); ht. add (C, c); ht. add (B, B); string s = (string) ht [A]; if (ht. contains (E) // file to determine whether a hash table Contains a specific key. The return value is true or false Console. writeLine (the E keyexist); ht. remove (C); // Remove a keyvalue pair from the Console. writeLine (ht [A]); // output a ht here. clear (); // remove all elements from the Console. writeLine (ht [A]); // file there will be no output here }}

3. traverse the hash table

DictionaryEntry Object is required to traverse a hash table. The Code is as follows:

For (DictionaryEntry de in ht) // fileht is a Hashtable instance {Console. WriteLine (de. Key );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.