Hashtable.Add 方法

将带有指定键和值的元素添加到 Hashtable 中。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Sub Add ( _
    key As Object, _
    value As Object _
)
用法
Dim instance As Hashtable
Dim key As Object
Dim value As Object

instance.Add(key, value)
public virtual void Add (
    Object key,
    Object value
)
public:
virtual void Add (
    Object^ key, 
    Object^ value
)
public void Add (
    Object key, 
    Object value
)
public function Add (
    key : Object, 
    value : Object
)

参数

  • key
    要添加的元素的键。
  • value
    要添加的元素的值。该值可以为 空引用(在 Visual Basic 中为 Nothing)。

异常

异常类型 条件

ArgumentNullException

key 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentException

Hashtable 中已存在具有相同键的元素。

NotSupportedException

Hashtable 为只读。

- 或 -

Hashtable 具有固定大小。

备注

键不能为 空引用(在 Visual Basic 中为 Nothing),但值可以。

其状态和哈希代码值之间不相关的对象通常不应用作键。例如,String 对象比 StringBuilder 对象更适于用作键。

通过设置 Hashtable 中不存在的键值(例如,myCollection["myNonexistentKey"] = myValue),还可以使用 Item 属性添加新元素。但是,如果指定的键已经存在于 Hashtable 中,则设置 Item 属性将改写旧值。相比之下,Add 方法不修改现有元素。

如果 Count 小于 Hashtable 的容量,则此方法的运算复杂度为 O(1)。如果需要增加容量以容纳新元素,则此方法成为 O(n) 运算,其中 n 为 Count

示例

下面的示例说明如何将元素添加到 Hashtable 中。

Imports System
Imports System.Collections

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 'Main

    Public Shared Sub PrintKeysAndValues(myHT As Hashtable)
        Console.WriteLine(vbTab + "-KEY-" + vbTab + "-VALUE-")
        Dim de As DictionaryEntry
        For Each de In  myHT
            Console.WriteLine(vbTab + "{0}:" + vbTab + "{1}", de.Key, de.Value)
        Next de
        Console.WriteLine()
    End Sub 'PrintKeysAndValues

End Class 'SamplesHashtable


' This code produces the following output.
' 
' The Hashtable contains the following:
'         -KEY-   -VALUE-
'         two:    quick
'         three:  brown
'         four:   fox
'         one:    The
' 
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 myHT )  {
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      foreach ( DictionaryEntry de in myHT )
         Console.WriteLine( "\t{0}:\t{1}", de.Key, de.Value );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The Hashtable contains the following:
        -KEY-   -VALUE-
        two:    quick
        three:  brown
        four:   fox
        one:    The
*/ 
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( Hashtable^ myHT );
int main()
{
   
   // Creates and initializes a new Hashtable.
   Hashtable^ myHT = gcnew 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 );
}

void PrintKeysAndValues( Hashtable^ myHT )
{
   Console::WriteLine( "\t-KEY-\t-VALUE-" );
   IEnumerator^ myEnum = myHT->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      DictionaryEntry de = *safe_cast<DictionaryEntry ^>(myEnum->Current);
      Console::WriteLine( "\t{0}:\t{1}", de.Key, de.Value );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The Hashtable contains the following:
         -KEY-   -VALUE-
         two:    quick
         three:  brown
         four:   fox
         one:    The
 */
import System.*;
import System.Collections.*;

public class SamplesHashtable
{
    public static void main(String[] args)
    {
        // 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);
    } //main

    public static void PrintKeysAndValues(Hashtable myHT)
    {
        Console.WriteLine("\t-KEY-\t-VALUE-");
        IEnumerator myEnumerator = myHT.GetEnumerator();

        while (myEnumerator.MoveNext()) {
            DictionaryEntry de = (DictionaryEntry)myEnumerator.get_Current();
            Console.WriteLine("\t{0}:\t{1}", de.get_Key(), de.get_Value());
        }
        Console.WriteLine();
    } //PrintKeysAndValues
} //SamplesHashtable

/* 
 This code produces the following output.
 
 The Hashtable contains the following:
         -KEY-   -VALUE-
         two:    quick
         three:  brown
         four:   fox
         one:    The
 */
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 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Hashtable 类
Hashtable 成员
System.Collections 命名空间
Remove
Item
IDictionary.Add