Usage of Hashtable in C #

Source: Internet
Author: User

First, start with the namespace

System.Collections

Then get to the point.

Common methods and properties of Hashtable:

01: Common Properties

Property name Description
Count Gets the number of key/value pairs contained in the Hashtable
Keys Gets the collection of keys contained in the Hashtable
Values Gets the collection that contains the values in the Hashtable

                           

02: Common Methods

return value type Method name Description
void ADD (Object key,object value) Adds the specified key and the merit element to the Hashtable
void Remove (Object Key) Remove an element with a specific key from the Hashtable
void Clear () Remove all elements from the Hashtable

Well, the light says no practice fake bashi, on the code

Class program    {        static void Main (string[] args)        {            //Create a Hashtable instance            Hashtable ht=new Hashtable () ;              Add the KeyValue key value to            ht. ADD ("A", "1");            Ht. ADD ("B", "2");            Ht. ADD ("C", "3");            Ht. ADD ("D", "4");             Traversing a hash table can only be traversed with foreach, because Hashtable cannot be accessed with an index
Traverse key
foreach (Object item in HT. Keys)
{
                Console.WriteLine (string) item);
            }
            Traverse value
            foreach (Object item in HT. Value)
{
                Console.WriteLine (string) item);
            }
 

foreach (DictionaryEntry de in HT) {Console.WriteLine ("Key--{0}; Value--{1}. ", De. Key, DE. Value); }
//hash table sort ArrayList akeys=new ArrayList (ht. Keys); Akeys. Sort (); foreach (string skey in Akeys) {Console.WriteLine ("{0, -15} {1, -15}", Skey, Ht[skey]); }//Determines if the hash table contains a specific key with a return value of TRUE or False if (HT. Contains ("a")) Console.WriteLine (ht["a"); Assign a value to the corresponding key ht["A"] = "Hello"; Removes a KeyValue key value pair ht. Remove ("C"); Traversal hash table foreach (DictionaryEntry de in HT) {Console.WriteLine ("Key--{0}; Value--{1}. ", De. Key, DE. Value); }//Remove all element ht. Clear (); There will be no output Console.WriteLine (ht["A"]); Console.readkey (); }}

Source: https://www.cnblogs.com/hfddz/p/6534235.html

Usage of Hashtable in C #

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.