c#中hashtable與hashmap

來源:互聯網
上載者:User
1.HashMap
does not exist in the c#。2.From the MSDN

using System;using System.Collections;class Example{    public static void Main()    {        // Create a new hash table.         //        Hashtable openWith = new Hashtable();        // Add some elements to the hash table. There are no          // duplicate keys, but some of the values are duplicates.        openWith.Add("txt", "notepad.exe");        openWith.Add("bmp", "paint.exe");        openWith.Add("dib", "paint.exe");        openWith.Add("rtf", "wordpad.exe");        // The Add method throws an exception if the new key is          // already in the hash table.         try        {            openWith.Add("txt", "winword.exe");        }        catch        {            Console.WriteLine("An element with Key = \"txt\" already exists.");        }        // The Item property is the default property, so you          // can omit its name when accessing elements.         Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);        // The default Item property can be used to change the value         // associated with a key.        openWith["rtf"] = "winword.exe";        Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);        // If a key does not exist, setting the default Item property         // for that key adds a new key/value pair.        openWith["doc"] = "winword.exe";        // ContainsKey can be used to test keys before inserting          // them.         if (!openWith.ContainsKey("ht"))        {            openWith.Add("ht", "hypertrm.exe");            Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);        }        // When you use foreach to enumerate hash table elements,         // the elements are retrieved as KeyValuePair objects.        Console.WriteLine();        foreach( DictionaryEntry de in openWith )        {            Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);        }        // To get the values alone, use the Values property.        ICollection valueColl = openWith.Values;        // The elements of the ValueCollection are strongly typed         // with the type that was specified for hash table values.        Console.WriteLine();        foreach( string s in valueColl )        {            Console.WriteLine("Value = {0}", s);        }        // To get the keys alone, use the Keys property.        ICollection keyColl = openWith.Keys;        // The elements of the KeyCollection are strongly typed         // with the type that was specified for hash table keys.        Console.WriteLine();        foreach( string s in keyColl )        {            Console.WriteLine("Key = {0}", s);        }        // Use the Remove method to remove a key/value pair.        Console.WriteLine("\nRemove(\"doc\")");        openWith.Remove("doc");        if (!openWith.ContainsKey("doc"))        {            Console.WriteLine("Key \"doc\" is not found.");        }    }}/* This code example produces the following output:An element with Key = "txt" already exists.For key = "rtf", value = wordpad.exe.For key = "rtf", value = winword.exe.Value added for key = "ht": hypertrm.exeKey = dib, Value = paint.exeKey = txt, Value = notepad.exeKey = ht, Value = hypertrm.exeKey = bmp, Value = paint.exeKey = rtf, Value = winword.exeKey = doc, Value = winword.exeValue = paint.exeValue = notepad.exeValue = hypertrm.exeValue = paint.exeValue = winword.exeValue = winword.exeKey = dibKey = txtKey = htKey = bmpKey = rtfKey = docRemove("doc")Key "doc" is not found. */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.