Introduction and simple usage of hash table (Hashtable) in C #

Source: Internet
Author: User

Keys are usually used for quick search, and keys are case-sensitive. values are used to store values corresponding to keys. In Hashtable, key/value pairs are of the object type, hashtable supports any type of key/value pairs <BR> Add a key/value key-value pair in the hash table: HashtableObject. add (key, value );
Remove a 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:

Copy codeThe Code is as follows: using System;

Using System. Collections; // This namespace must be introduced when Hashtable is used.

Class hashtable

{

Public static void Main ()

{

Hashtable ht = new Hashtable (); // create a Hashtable instance

Ht. Add ("E", "e"); // Add key/value pairs

Ht. Add ("A", "");

Ht. Add ("C", "c ");

Ht. Add ("B", "B ");

String s = (string) ht ["A"];

If (ht. Contains ("E") // checks whether the hash table Contains a specific key. The return value is true or false.

Console. WriteLine ("the E key: exist ");

Ht. Remove ("C"); // Remove a key/value pair

Console. WriteLine (ht ["A"]); // output
}

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

For (DictionaryEntry de in ht) // ht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key-value Pair
Console. WriteLine (de. Value); // de. Key corresponds to the key/value Key-value Pair Value
}
}

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.