最終更新:2019-01-15 (火) 18:15:19 (1927d)  

System.Collections.Hashtable
Top / System.Collections.Hashtable

キーのハッシュ コードに基づいて編成された、キー/値ペアのコレクションを表します。

メソッド

  • Hashtable.Add?(Object, Object)指定したキーおよび値を持つ要素を Hashtable に追加します。

サンプル

Hashtable ht = new Hashtable(); // ハッシュテーブルを用意
/* 値を格納していく */
ht.Add("Dolphin", "海豚");
ht.Add("Dog", "犬");
ht.Add("Cat", "猫");
ht.Add(1, "ONE");
ht.Add(2, "TWO");
ht.Add(3.14, "PI");

/* 次のような格納方法もある */
ht[true] = false;
ht[false] = true;

Console.WriteLine("英単語をキーに、漢字データを取り出す");
Console.WriteLine("Dolphin:{0}\nDog:{1}\nCat:{2}\n", ht["Dolphin"], ht["Dog"], ht["Cat"]);
Console.WriteLine("数字をキーに、英単語データを取り出す");
Console.WriteLine("1:{0}\n2:{1}\n3.14:{2}\n", ht[1], ht[2],ht[3.14]);
Console.WriteLine("真偽値をキーに、逆の真偽値を取り出す");
Console.WriteLine("True:{0}\nFalse:{1}", ht[true], ht[false]);
 
Dim hash As New Hashtable()

' Add some elements to the hash table. There are no 
' duplicate keys, but some of the values are duplicates.
hash.Add("txt", "notepad.exe")
hash.Add("bmp", "paint.exe")
hash.Add("dib", "paint.exe")
hash.Add("rtf", "wordpad.exe")

' The Add method throws an exception if the new key is 
' already in the hash table.
Try
    hash.Add("txt", "winword.exe")
Catch
    Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try

' The Item property is the default property, so you 
' can omit its name when accessing elements. 
Console.WriteLine("For key = ""rtf"", value = {0}.", hash("rtf"))

For Each de As DictionaryEntry In hash
    Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value)
Next de

Implements