Hashtable.Add メソッド
指定したキーおよび値を持つ要素を Hashtable に追加します。
Public Overridable Sub Add( _
ByVal key As Object, _ ByVal value As Object _) Implements IDictionary.Add
[C#]
public virtual void Add(objectkey,objectvalue);
[C++]
public: virtual void Add(Object* key,Object* value);
[JScript]
public function Add(
key : Object,value : Object);
パラメータ
- key
追加する要素のキー。 - value
追加する要素の値。値は null 参照 (Visual Basic では Nothing) に設定できます。
実装
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | key が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | 同じキーを持つ要素が、 Hashtable に既に存在します。 |
NotSupportedException | Hashtable が読み取り専用です。
または Hashtable が固定サイズです。 |
解説
キーには null 参照 (Visual Basic では Nothing) は使用できませんが、値は null でもかまいません。
状態とハッシュ コード値との間に相関関係のないオブジェクトは、通常はキーとして使用しないでください。たとえば、StringBuilder オブジェクトよりも String オブジェクトの方が、キーとして使用するには適しています。
Item プロパティを使用すると、 Hashtable 内に存在しないキーの値を設定することで、新しい要素を追加することもできます。たとえば、 myCollection["myNonexistentKey"] = myValue
のように使用します。ただし、指定したキーが Hashtable 内に既に存在する場合、 Item プロパティを設定すると既存の値が上書きされます。対照的に、 Add メソッドは既存の要素を変更しません。
使用例
Hashtable に要素を追加する方法の例を次に示します。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class SamplesHashtable
Public Shared Sub Main()
' Creates and initializes a new Hashtable.
Dim myHT As New Hashtable()
myHT.Add("one", "The")
myHT.Add("two", "quick")
myHT.Add("three", "brown")
myHT.Add("four", "fox")
' Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:")
PrintKeysAndValues(myHT)
End Sub
Public Shared Sub PrintKeysAndValues(myList As Hashtable)
Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
+ "-VALUE-")
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
+ "{1}", myEnumerator.Key, myEnumerator.Value)
End While
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The Hashtable contains the following:
' -KEY- -VALUE-
' three: brown
' four: fox
' two: quick
' one: The
[C#]
using System;
using System.Collections;
public class SamplesHashtable {
public static void Main() {
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add( "one", "The" );
myHT.Add( "two", "quick" );
myHT.Add( "three", "brown" );
myHT.Add( "four", "fox" );
// Displays the Hashtable.
Console.WriteLine( "The Hashtable contains the following:" );
PrintKeysAndValues( myHT );
}
public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
The Hashtable contains the following:
-KEY- -VALUE-
three: brown
four: fox
two: quick
one: The
*/
[C++]
#using <system.dll>
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( Hashtable* myList ) {
IDictionaryEnumerator* myEnumerator = myList->GetEnumerator();
Console::WriteLine( S"\t-KEY-\t-VALUE-" );
while ( myEnumerator->MoveNext() )
Console::WriteLine( S"\t{0}:\t{1}", myEnumerator->Key, myEnumerator->Value );
Console::WriteLine();
}
void main() {
// Creates and initializes a new Hashtable.
Hashtable* myHT = new Hashtable();
myHT->Add( S"one", S"The" );
myHT->Add( S"two", S"quick" );
myHT->Add( S"three", S"brown" );
myHT->Add( S"four", S"fox" );
// Displays the Hashtable.
Console::WriteLine( S"The Hashtable contains the following:" );
PrintKeysAndValues( myHT );
}
/*
This code produces the following output.
The Hashtable contains the following:
-KEY- -VALUE-
three: brown
four: fox
two: quick
one: The
*/
[JScript]
import System
import System.Collections
// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add("one", "The")
myHT.Add("two", "quick")
myHT.Add("three", "brown")
myHT.Add("four", "fox")
// Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:")
PrintKeysAndValues(myHT)
function PrintKeysAndValues(myList : Hashtable){
var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
Console.WriteLine("\t-KEY-\t-VALUE-")
while(myEnumerator.MoveNext())
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value)
Console.WriteLine()
}
// This code produces the following output.
//
// The Hashtable contains the following:
// -KEY- -VALUE-
// three: brown
// four: fox
// two: quick
// one: The
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
Hashtable クラス | Hashtable メンバ | System.Collections 名前空間 | Remove | Item | IDictionary.Add