Hashtable.Add(Object, Object) 메서드

정의

지정된 키와 값을 Hashtable가진 요소를 에 추가합니다.

public:
 virtual void Add(System::Object ^ key, System::Object ^ value);
public virtual void Add(object key, object value);
public virtual void Add(object key, object? value);
abstract member Add : obj * obj -> unit
override this.Add : obj * obj -> unit
Public Overridable Sub Add (key As Object, value As Object)

매개 변수

key
Object

추가할 요소의 키입니다.

value
Object

추가할 요소의 값입니다. 값은 .일 null수 있습니다.

구현

예외

keynull입니다.

키가 같은 요소가 이미 있습니다 Hashtable.

읽기 Hashtable 전용입니다.

-또는-

크기가 Hashtable 고정되어 있습니다.

예제

다음 예제에서는 요소를 에 추가하는 방법을 보여 있습니다 Hashtable.

using System;
using System.Collections;
public class SamplesHashtable
{

   public static void Main()
   {
      // Creates and initializes a new Hashtable.
      var 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{de.Key}:\t{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
*/
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

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

End Class


' This code produces the following output.
' 
' The Hashtable contains the following:
'         -KEY-   -VALUE-
'         two:    quick
'         one:    The
'         three:  brown
'         four:   fox
'

설명

키는 될 null수 없지만 값은 될 수 있습니다.

해당 상태와 해시 코드 값 간에 상관 관계가 없는 개체는 일반적으로 키로 사용해서는 안 됩니다. 예를 들어 String 개체는 키로 사용하기 위해 StringBuilder 개체보다 낫습니다.

에 없는 Item[]키의 값을 설정 하 여 새 요소를 추가 하는 속성을 사용할 Hashtable 수도 있습니다. 예를 들어. myCollection["myNonexistentKey"] = myValue 그러나 지정된 키가 이미 있는 Hashtable경우 속성을 설정 Item[] 하면 이전 값이 덮어씁니다. 반면, 메서드는 Add 기존 요소를 수정하지 않습니다.

용량Count보다 작으면 Hashtable 이 메서드는 작업입니다O(1). 새 요소를 수용하기 위해 용량을 늘려야 하는 경우 이 메서드는 O(n) 작업이 nCount됩니다.

적용 대상

추가 정보