Hashtable 建構函式

定義

初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class.

多載

Hashtable()

使用預設初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the default initial capacity, load factor, hash code provider, and comparer.

Hashtable(Int32, Single, IHashCodeProvider, IComparer)

使用指定的初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, load factor, hash code provider, and comparer.

Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數、雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the specified load factor, hash code provider, and comparer.

Hashtable(Int32, Single, IEqualityComparer)

使用指定的初始容量、載入因數和 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, load factor, and IEqualityComparer object.

Hashtable(Int32, IHashCodeProvider, IComparer)

使用指定的初始容量、雜湊程式碼提供者、比較子和預設的載入因數,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, hash code provider, comparer, and the default load factor.

Hashtable(IDictionary, Single, IEqualityComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數和 IEqualityComparer 物件。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the specified load factor and IEqualityComparer object.

Hashtable(IDictionary, IHashCodeProvider, IComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數,以及指定的雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor, and the specified hash code provider and comparer. 這個應用程式開發介面已過時。This API is obsolete. 請參閱 Hashtable(IDictionary, IEqualityComparer) 以瞭解替代步驟。For an alternative, see Hashtable(IDictionary, IEqualityComparer).

Hashtable(Int32, Single)

使用指定的初始容量和載入因數,以及預設的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity and load factor, and the default hash code provider and comparer.

Hashtable(SerializationInfo, StreamingContext)

初始化 Hashtable 類別的空白新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfoStreamingContext 物件。Initializes a new, empty instance of the Hashtable class that is serializable using the specified SerializationInfo and StreamingContext objects.

Hashtable(IHashCodeProvider, IComparer)

使用預設的初始容量和載入因數,以及指定的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the default initial capacity and load factor, and the specified hash code provider and comparer.

Hashtable(IDictionary, Single)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數,以及預設的雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the specified load factor, and the default hash code provider and comparer.

Hashtable(IDictionary, IEqualityComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to a new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數以及指定的 IEqualityComparer 物件。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor and the specified IEqualityComparer object.

Hashtable(Int32)

使用指定的初始容量以及預設的載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, and the default load factor, hash code provider, and comparer.

Hashtable(IEqualityComparer)

使用預設的初始容量和載入因數,以及指定的 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the default initial capacity and load factor, and the specified IEqualityComparer object.

Hashtable(IDictionary)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設載入因數、雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor, hash code provider, and comparer.

Hashtable(Int32, IEqualityComparer)

使用指定的初始容量和 Hashtable,以及預設的載入因數,初始化 IEqualityComparer 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity and IEqualityComparer, and the default load factor.

Hashtable()

使用預設初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the default initial capacity, load factor, hash code provider, and comparer.

public:
 Hashtable();
public Hashtable ();
Public Sub New ()

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myComparer : IEqualityComparer
{
public:
    virtual bool Equals(Object^ x, Object^ y) 
    {
        return x->Equals(y);
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

int main()
{
   
   // Create a hash table using the default hash code provider and the default comparer.
   Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
   myHT1->Add( "FIRST", "Hello" );
   myHT1->Add( "SECOND", "World" );
   myHT1->Add( "THIRD", "!" );
   
   // Create a hash table using the specified IEqualityComparer that uses
   // the default Object.Equals to determine equality.
   Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
   myHT2->Add( "FIRST", "Hello" );
   myHT2->Add( "SECOND", "World" );
   myHT2->Add( "THIRD", "!" );
   
   // Create a hash table using a case-insensitive hash code provider and
   // case-insensitive comparer based on the InvariantCulture.
   Hashtable^ myHT3 = gcnew Hashtable(
            CaseInsensitiveHashCodeProvider::DefaultInvariant,
            CaseInsensitiveComparer::DefaultInvariant);
   myHT3->Add( "FIRST", "Hello" );
   myHT3->Add( "SECOND", "World" );
   myHT3->Add( "THIRD", "!" );
   
   // Create a hash table using an IEqualityComparer that is based on
   // the Turkish culture (tr-TR) where "I" is not the uppercase
   // version of "i".
   CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
   Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
   myHT4->Add( "FIRST", "Hello" );
   myHT4->Add( "SECOND", "World" );
   myHT4->Add( "THIRD", "!" );
   
   // Search for a key in each hash table.
   Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
using System;
using System.Collections;
using System.Globalization;

class myComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable();
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the default Object.Equals to determine equality.
        var myHT2 = new Hashtable(new myComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using a case-insensitive hash code provider and
        // case-insensitive comparer based on the InvariantCulture.
        Hashtable myHT3 = new Hashtable(
            CaseInsensitiveHashCodeProvider.DefaultInvariant,
            CaseInsensitiveComparer.DefaultInvariant);
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");
        myHT4.Add("SECOND", "World");
        myHT4.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myComparer
    Implements IEqualityComparer
    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return x.Equals(y)
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode

        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable()
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the default Object.Equals to determine equality.
        Dim myHT2 As New Hashtable(New myComparer())
        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using a case-insensitive hash code provider and
        ' case-insensitive comparer based on the InvariantCulture.
        Dim myHT3 As New Hashtable( _
            CaseInsensitiveHashCodeProvider.DefaultInvariant, _
            CaseInsensitiveComparer.DefaultInvariant)
        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
        myHT4.Add("FIRST", "Hello")
        myHT4.Add("SECOND", "World")
        myHT4.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")

    End Sub

End Class

'This code produces the following output.
'Results vary depending on the system's culture settings.

'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False

備註

雜湊表的容量可用來根據負載因數來計算雜湊表值區的最佳數目。A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. 容量會視需要自動增加。Capacity is automatically increased as required.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

此函式是 O(1) 作業。This constructor is an O(1) operation.

另請參閱

Hashtable(Int32, Single, IHashCodeProvider, IComparer)

警告

此 API 現已淘汰。

使用指定的初始容量、載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, load factor, hash code provider, and comparer.

public:
 Hashtable(int capacity, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[System.Obsolete("Please use Hashtable(int, float, IEqualityComparer) instead.")]
public Hashtable (int capacity, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
new System.Collections.Hashtable : int * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)

參數

capacity
Int32

Hashtable 物件一開始可包含項目的大約數目。The approximate number of elements that the Hashtable object can initially contain.

loadFactor
Single

從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. 結果是雜湊桶項目的最大比例。The result is the maximum ratio of elements to buckets.

hcp
IHashCodeProvider

IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

-或--or- 要使用預設雜湊程式碼提供者的 null 是每個索引鍵的 GetHashCode() 之實作。null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

要用來判斷兩個索引鍵是否相等的 IComparer 物件。The IComparer object to use to determine whether two keys are equal.

-或--or- 表示使用預設比較子的 null,預設比較子是每個索引鍵的 Equals(Object) 實作。null to use the default comparer, which is each key's implementation of Equals(Object).

屬性

例外狀況

capacity 小於零。capacity is less than zero.

-或--or- loadFactor 小於 0.1。loadFactor is less than 0.1.

-或--or- loadFactor 大於 1.0。loadFactor is greater than 1.0.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3, .8f);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));

        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer())

        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer(myCul))

        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

    End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

指定初始容量,就不需要在將元素加入至 Hashtable 物件時執行數個調整大小作業。Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption. 1.0 的負載因數是速度與大小之間的最佳平衡。A load factor of 1.0 is the best balance between speed and size.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

自訂雜湊程式碼提供者和自訂比較子可啟用使用不區分大小寫字串進行查閱的案例。The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

此函式是 O(n) 運算,其中 ncapacity 參數。This constructor is an O(n) operation, where n is the capacity parameter.

另請參閱

Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)

警告

此 API 現已淘汰。

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數、雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the specified load factor, hash code provider, and comparer.

public:
 Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[System.Obsolete("Please use Hashtable(IDictionary, float, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, hcp As IHashCodeProvider, comparer As IComparer)

參數

d
IDictionary

要複製到新 IDictionary 物件的 Hashtable 物件。The IDictionary object to copy to a new Hashtable object.

loadFactor
Single

從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. 結果是雜湊桶項目的最大比例。The result is the maximum ratio of elements to buckets.

hcp
IHashCodeProvider

IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

-或--or- 要使用預設雜湊程式碼提供者的 null 是每個索引鍵的 GetHashCode() 之實作。null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

要用來判斷兩個索引鍵是否相等的 IComparer 物件。The IComparer object to use to determine whether two keys are equal.

-或--or- 表示使用預設比較子的 null,預設比較子是每個索引鍵的 Equals(Object) 實作。null to use the default comparer, which is each key's implementation of Equals(Object).

屬性

例外狀況

dnulld is null.

loadFactor 小於 0.1。loadFactor is less than 0.1.

-或--or- loadFactor 大於 1.0。loadFactor is greater than 1.0.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
       
       // Create the dictionary.
       SortedList^ mySL = gcnew SortedList;
       mySL->Add( "FIRST", "Hello" );
       mySL->Add( "SECOND", "World" );
       mySL->Add( "THIRD", "!" );
       
       // Create a hash table using the default hash code provider and the default comparer.
       Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
       
       // Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
       Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
       
       // Create a hash table using the specified KeyComparer.
       // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
       // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
       CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
       Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
       
       // Search for a key in each hash table.
       Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(mySL, .8f);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(mySL, .8f, 
            new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

初始容量會設定為來源字典中的元素數目。The initial capacity is set to the number of elements in the source dictionary. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption. 1.0 的負載因數是速度與大小之間的最佳平衡。A load factor of 1.0 is the best balance between speed and size.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

自訂雜湊程式碼提供者和自訂比較子可啟用使用不區分大小寫字串進行查閱的案例。The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Hashtable 的元素會以列舉值逐一查看 IDictionary 物件的相同順序排序。The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

此函式是 O(n) 運算,其中 nd 參數中的元素數目。This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Hashtable(Int32, Single, IEqualityComparer)

使用指定的初始容量、載入因數和 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, load factor, and IEqualityComparer object.

public:
 Hashtable(int capacity, float loadFactor, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (int capacity, float loadFactor, System.Collections.IEqualityComparer equalityComparer);
new System.Collections.Hashtable : int * single * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single, equalityComparer As IEqualityComparer)

參數

capacity
Int32

Hashtable 物件一開始可包含項目的大約數目。The approximate number of elements that the Hashtable object can initially contain.

loadFactor
Single

從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. 結果是雜湊桶項目的最大比例。The result is the maximum ratio of elements to buckets.

equalityComparer
IEqualityComparer

IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

-或--or- null 表示使用預設雜湊程式碼提供者和預設比較子。null to use the default hash code provider and the default comparer. 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

例外狀況

capacity 小於零。capacity is less than zero.

-或--or- loadFactor 小於 0.1。loadFactor is less than 0.1.

-或--or- loadFactor 大於 1.0。loadFactor is greater than 1.0.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3, .8f);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));

        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer())

        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer(myCul))

        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

    End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

指定初始容量,就不需要在將元素加入至 Hashtable 物件時執行數個調整大小作業。Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption. 1.0 的負載因數是速度與大小之間的最佳平衡。A load factor of 1.0 is the best balance between speed and size.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

IEqualityComparer 物件同時包含雜湊碼提供者和比較子。The IEqualityComparer object includes both the hash code provider and the comparer. 如果 Hashtable 的函式中使用了 IEqualityComparer,就不需要在 Hashtable 中當做索引鍵使用的物件來覆寫 Object.GetHashCodeObject.Equals 方法。If an IEqualityComparer is used in the Hashtable constructor, the objects used as keys in the Hashtable are not required to override the Object.GetHashCode and Object.Equals methods.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

IEqualityComparer 可以啟用使用不區分大小寫字串進行查閱的案例。The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

此函式是 O(n) 運算,其中 ncapacity 參數。This constructor is an O(n) operation, where n is the capacity parameter.

另請參閱

Hashtable(Int32, IHashCodeProvider, IComparer)

警告

此 API 現已淘汰。

使用指定的初始容量、雜湊程式碼提供者、比較子和預設的載入因數,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, hash code provider, comparer, and the default load factor.

public:
 Hashtable(int capacity, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
public Hashtable (int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[System.Obsolete("Please use Hashtable(int, IEqualityComparer) instead.")]
public Hashtable (int capacity, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
new System.Collections.Hashtable : int * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, hcp As IHashCodeProvider, comparer As IComparer)

參數

capacity
Int32

Hashtable 物件一開始可包含項目的大約數目。The approximate number of elements that the Hashtable object can initially contain.

hcp
IHashCodeProvider

IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

-或--or- 要使用預設雜湊程式碼提供者的 null 是每個索引鍵的 GetHashCode() 之實作。null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

要用來判斷兩個索引鍵是否相等的 IComparer 物件。The IComparer object to use to determine whether two keys are equal.

-或--or- 表示使用預設比較子的 null,預設比較子是每個索引鍵的 Equals(Object) 實作。null to use the default comparer, which is each key's implementation of Equals(Object).

屬性

例外狀況

capacity 小於零。capacity is less than zero.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(3)
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(3, New myCultureComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

指定初始容量,就不需要在將元素加入至 Hashtable 物件時執行數個調整大小作業。Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

自訂雜湊程式碼提供者和自訂比較子可啟用使用不區分大小寫字串進行查閱的案例。The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

此函式是 O(n) 運算,其中 ncapacity 參數。This constructor is an O(n) operation, where n is the capacity parameter.

另請參閱

Hashtable(IDictionary, Single, IEqualityComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數和 IEqualityComparer 物件。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the specified load factor and IEqualityComparer object.

public:
 Hashtable(System::Collections::IDictionary ^ d, float loadFactor, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IDictionary d, float loadFactor, System.Collections.IEqualityComparer equalityComparer);
new System.Collections.Hashtable : System.Collections.IDictionary * single * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single, equalityComparer As IEqualityComparer)

參數

d
IDictionary

要複製到新 IDictionary 物件的 Hashtable 物件。The IDictionary object to copy to a new Hashtable object.

loadFactor
Single

從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. 結果是雜湊桶項目的最大比例。The result is the maximum ratio of elements to buckets.

equalityComparer
IEqualityComparer

IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

-或--or- null 表示使用預設雜湊程式碼提供者和預設比較子。null to use the default hash code provider and the default comparer. 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

例外狀況

dnulld is null.

loadFactor 小於 0.1。loadFactor is less than 0.1.

-或--or- loadFactor 大於 1.0。loadFactor is greater than 1.0.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
       
       // Create the dictionary.
       SortedList^ mySL = gcnew SortedList;
       mySL->Add( "FIRST", "Hello" );
       mySL->Add( "SECOND", "World" );
       mySL->Add( "THIRD", "!" );
       
       // Create a hash table using the default hash code provider and the default comparer.
       Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
       
       // Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
       Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
       
       // Create a hash table using the specified KeyComparer.
       // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
       // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
       CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
       Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
       
       // Search for a key in each hash table.
       Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(mySL, .8f);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(mySL, .8f, 
            new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

初始容量會設定為來源字典中的元素數目。The initial capacity is set to the number of elements in the source dictionary. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption. 1.0 的負載因數是速度與大小之間的最佳平衡。A load factor of 1.0 is the best balance between speed and size.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

IEqualityComparer 物件同時包含雜湊碼提供者和比較子。The IEqualityComparer object includes both the hash code provider and the comparer. 如果在 Hashtable 的函式中使用了 IEqualityComparer,在 Hashtable 物件中當做索引鍵使用的物件,就不需要覆寫 Object.GetHashCodeObject.Equals 方法。If an IEqualityComparer is used in the Hashtable constructor, the objects used as keys in the Hashtable object are not required to override the Object.GetHashCode and Object.Equals methods.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

IEqualityComparer 可以啟用使用不區分大小寫字串進行查閱的案例。The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Hashtable 的元素會以列舉值逐一查看 IDictionary 物件的相同順序排序。The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

此函式是 O(n) 運算,其中 nd 參數中的元素數目。This constructor is an O(n) operation, where n is the number of elements in the d parameter.

另請參閱

Hashtable(IDictionary, IHashCodeProvider, IComparer)

警告

此 API 現已淘汰。

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數,以及指定的雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor, and the specified hash code provider and comparer. 這個應用程式開發介面已過時。This API is obsolete. 請參閱 Hashtable(IDictionary, IEqualityComparer) 以瞭解替代步驟。For an alternative, see Hashtable(IDictionary, IEqualityComparer).

public:
 Hashtable(System::Collections::IDictionary ^ d, System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[System.Obsolete("Please use Hashtable(IDictionary, IEqualityComparer) instead.")]
public Hashtable (System.Collections.IDictionary d, System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, hcp As IHashCodeProvider, comparer As IComparer)

參數

d
IDictionary

要複製到新 IDictionary 物件的 Hashtable 物件。The IDictionary object to copy to a new Hashtable object.

hcp
IHashCodeProvider

IHashCodeProvider 物件,它會為 Hashtable 中的所有索引鍵提供雜湊程式碼。The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

-或--or- 要使用預設雜湊程式碼提供者的 null 是每個索引鍵的 GetHashCode() 之實作。null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

要用來判斷兩個索引鍵是否相等的 IComparer 物件。The IComparer object to use to determine whether two keys are equal.

-或--or- 表示使用預設比較子的 null,預設比較子是每個索引鍵的 Equals(Object) 實作。null to use the default comparer, which is each key's implementation of Equals(Object).

屬性

例外狀況

dnulld is null.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create the dictionary.
        SortedList^ mySL = gcnew SortedList();
        mySL->Add("FIRST", "Hello");
        mySL->Add("SECOND", "World");
        mySL->Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
    }
};

int main()
{
    SamplesHashtable::Main();
};

/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        var mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        var myHT2 = new Hashtable(mySL, new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
    }
}


/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL)

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

初始容量會設定為來源字典中的元素數目。The initial capacity is set to the number of elements in the source dictionary. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

自訂雜湊程式碼提供者和自訂比較子可啟用使用不區分大小寫字串進行查閱的案例。The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Hashtable 的元素會以列舉值逐一查看 IDictionary 物件的相同順序排序。The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

此函式是 O(n) 運算,其中 nd 參數中的元素數目。This constructor is an O(n) operation, where n is the number of elements in the d parameter.

另請參閱

Hashtable(Int32, Single)

使用指定的初始容量和載入因數,以及預設的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity and load factor, and the default hash code provider and comparer.

public:
 Hashtable(int capacity, float loadFactor);
public Hashtable (int capacity, float loadFactor);
new System.Collections.Hashtable : int * single -> System.Collections.Hashtable
Public Sub New (capacity As Integer, loadFactor As Single)

參數

capacity
Int32

Hashtable 物件一開始可包含項目的大約數目。The approximate number of elements that the Hashtable object can initially contain.

loadFactor
Single

從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. 結果是雜湊桶項目的最大比例。The result is the maximum ratio of elements to buckets.

例外狀況

capacity 小於零。capacity is less than zero.

-或--or- loadFactor 小於 0.1。loadFactor is less than 0.1.

-或--or- loadFactor 大於 1.0。loadFactor is greater than 1.0.

capacity 導致溢位。capacity is causing an overflow.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3, .8f);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, .8f, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, .8f, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3, .8f);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, .8f, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, .8f, new myCultureComparer(myCul));

        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable(3, System.Convert.ToSingle(0.8))
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Dim myHT2 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer())

        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT3 As New Hashtable(3, System.Convert.ToSingle(0.8), _
            New myCultureComparer(myCul))

        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

    End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

指定初始容量,就不需要在將元素加入至 Hashtable 物件時執行數個調整大小作業。Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption. 1.0 的負載因數是速度與大小之間的最佳平衡。A load factor of 1.0 is the best balance between speed and size.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

此函式是 O(n) 運算,其中 ncapacity 參數。This constructor is an O(n) operation, where n is the capacity parameter.

另請參閱

Hashtable(SerializationInfo, StreamingContext)

初始化 Hashtable 類別的空白新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfoStreamingContext 物件。Initializes a new, empty instance of the Hashtable class that is serializable using the specified SerializationInfo and StreamingContext objects.

protected:
 Hashtable(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected Hashtable (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Collections.Hashtable : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Hashtable
Protected Sub New (info As SerializationInfo, context As StreamingContext)

參數

info
SerializationInfo

SerializationInfo 物件,含有要序列化 Hashtable 物件的必要資訊。A SerializationInfo object containing the information required to serialize the Hashtable object.

context
StreamingContext

StreamingContext 物件,包含 Hashtable 所關聯之序列化資料流的來源及目的地。A StreamingContext object containing the source and destination of the serialized stream associated with the Hashtable.

例外狀況

infonullinfo is null.

備註

雜湊表的容量可用來根據負載因數來計算雜湊表值區的最佳數目。A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. 容量會視需要自動增加。Capacity is automatically increased as required.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

此函式是 O(n) 作業,其中 nCountThis constructor is an O(n) operation, where n is Count.

因為序列化和還原序列化 Hashtable 的列舉值可能會導致元素重新排序,所以不能在不呼叫 Reset 方法的情況下繼續列舉。Because serializing and deserializing an enumerator for a Hashtable can cause the elements to become reordered, it is not possible to continue enumeration without calling the Reset method.

另請參閱

Hashtable(IHashCodeProvider, IComparer)

警告

此 API 現已淘汰。

使用預設的初始容量和載入因數,以及指定的雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the default initial capacity and load factor, and the specified hash code provider and comparer.

public:
 Hashtable(System::Collections::IHashCodeProvider ^ hcp, System::Collections::IComparer ^ comparer);
public Hashtable (System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
[System.Obsolete("Please use Hashtable(IEqualityComparer) instead.")]
public Hashtable (System.Collections.IHashCodeProvider hcp, System.Collections.IComparer comparer);
new System.Collections.Hashtable : System.Collections.IHashCodeProvider * System.Collections.IComparer -> System.Collections.Hashtable
Public Sub New (hcp As IHashCodeProvider, comparer As IComparer)

參數

hcp
IHashCodeProvider

IHashCodeProvider 物件,為 Hashtable 物件中的所有索引鍵提供雜湊程式碼。The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable object.

-或--or- 要使用預設雜湊程式碼提供者的 null 是每個索引鍵的 GetHashCode() 之實作。null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

要用來判斷兩個索引鍵是否相等的 IComparer 物件。The IComparer object to use to determine whether two keys are equal.

-或--or- 表示使用預設比較子的 null,預設比較子是每個索引鍵的 Equals(Object) 實作。null to use the default comparer, which is each key's implementation of Equals(Object).

屬性

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myComparer : IEqualityComparer
{
public:
    virtual bool Equals(Object^ x, Object^ y) 
    {
        return x->Equals(y);
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

int main()
{
   
   // Create a hash table using the default hash code provider and the default comparer.
   Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
   myHT1->Add( "FIRST", "Hello" );
   myHT1->Add( "SECOND", "World" );
   myHT1->Add( "THIRD", "!" );
   
   // Create a hash table using the specified IEqualityComparer that uses
   // the default Object.Equals to determine equality.
   Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
   myHT2->Add( "FIRST", "Hello" );
   myHT2->Add( "SECOND", "World" );
   myHT2->Add( "THIRD", "!" );
   
   // Create a hash table using a case-insensitive hash code provider and
   // case-insensitive comparer based on the InvariantCulture.
   Hashtable^ myHT3 = gcnew Hashtable(
            CaseInsensitiveHashCodeProvider::DefaultInvariant,
            CaseInsensitiveComparer::DefaultInvariant);
   myHT3->Add( "FIRST", "Hello" );
   myHT3->Add( "SECOND", "World" );
   myHT3->Add( "THIRD", "!" );
   
   // Create a hash table using an IEqualityComparer that is based on
   // the Turkish culture (tr-TR) where "I" is not the uppercase
   // version of "i".
   CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
   Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
   myHT4->Add( "FIRST", "Hello" );
   myHT4->Add( "SECOND", "World" );
   myHT4->Add( "THIRD", "!" );
   
   // Search for a key in each hash table.
   Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
using System;
using System.Collections;
using System.Globalization;

class myComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable();
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the default Object.Equals to determine equality.
        var myHT2 = new Hashtable(new myComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using a case-insensitive hash code provider and
        // case-insensitive comparer based on the InvariantCulture.
        Hashtable myHT3 = new Hashtable(
            CaseInsensitiveHashCodeProvider.DefaultInvariant,
            CaseInsensitiveComparer.DefaultInvariant);
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");
        myHT4.Add("SECOND", "World");
        myHT4.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myComparer
    Implements IEqualityComparer
    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return x.Equals(y)
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode

        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable()
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the default Object.Equals to determine equality.
        Dim myHT2 As New Hashtable(New myComparer())
        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using a case-insensitive hash code provider and
        ' case-insensitive comparer based on the InvariantCulture.
        Dim myHT3 As New Hashtable( _
            CaseInsensitiveHashCodeProvider.DefaultInvariant, _
            CaseInsensitiveComparer.DefaultInvariant)
        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
        myHT4.Add("FIRST", "Hello")
        myHT4.Add("SECOND", "World")
        myHT4.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")

    End Sub

End Class

'This code produces the following output.
'Results vary depending on the system's culture settings.

'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False

備註

雜湊表的容量可用來根據負載因數來計算雜湊表值區的最佳數目。A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. 容量會視需要自動增加。Capacity is automatically increased as required.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

自訂雜湊程式碼提供者和自訂比較子可啟用使用不區分大小寫字串進行查閱的案例。The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

此函式是 O(1) 作業。This constructor is an O(1) operation.

另請參閱

Hashtable(IDictionary, Single)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用指定的載入因數,以及預設的雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the specified load factor, and the default hash code provider and comparer.

public:
 Hashtable(System::Collections::IDictionary ^ d, float loadFactor);
public Hashtable (System.Collections.IDictionary d, float loadFactor);
new System.Collections.Hashtable : System.Collections.IDictionary * single -> System.Collections.Hashtable
Public Sub New (d As IDictionary, loadFactor As Single)

參數

d
IDictionary

要複製到新 IDictionary 物件的 Hashtable 物件。The IDictionary object to copy to a new Hashtable object.

loadFactor
Single

從 0.1 到 1.0 範圍中的數字乘以預設值,提供最佳效能。A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. 結果是雜湊桶項目的最大比例。The result is the maximum ratio of elements to buckets.

例外狀況

dnulld is null.

loadFactor 小於 0.1。loadFactor is less than 0.1.

-或--or- loadFactor 大於 1.0。loadFactor is greater than 1.0.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
       
       // Create the dictionary.
       SortedList^ mySL = gcnew SortedList;
       mySL->Add( "FIRST", "Hello" );
       mySL->Add( "SECOND", "World" );
       mySL->Add( "THIRD", "!" );
       
       // Create a hash table using the default hash code provider and the default comparer.
       Hashtable^ myHT1 = gcnew Hashtable( mySL, .8f );
       
       // Create a hash table using the specified case-insensitive hash code provider and case-insensitive comparer.
       Hashtable^ myHT2 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer() );
       
       // Create a hash table using the specified KeyComparer.
       // The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
       // which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
       CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
       Hashtable^ myHT3 = gcnew Hashtable( mySL, .8f, gcnew myCultureComparer( myCul ) );
       
       // Search for a key in each hash table.
       Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
       Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(mySL, .8f);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(mySL, .8f, 
            new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, .8f, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL, System.Convert.ToSingle(0.8))

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, System.Convert.ToSingle(0.8), _
        New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

初始容量會設定為來源字典中的元素數目。The initial capacity is set to the number of elements in the source dictionary. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption. 1.0 的負載因數是速度與大小之間的最佳平衡。A load factor of 1.0 is the best balance between speed and size.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

Hashtable 的元素會以列舉值逐一查看 IDictionary 物件的相同順序排序。The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

此函式是 O(n) 運算,其中 nd 參數中的元素數目。This constructor is an O(n) operation, where n is the number of elements in the d parameter.

另請參閱

Hashtable(IDictionary, IEqualityComparer)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to a new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設的載入因數以及指定的 IEqualityComparer 物件。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor and the specified IEqualityComparer object.

public:
 Hashtable(System::Collections::IDictionary ^ d, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IDictionary d, System.Collections.IEqualityComparer equalityComparer);
new System.Collections.Hashtable : System.Collections.IDictionary * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (d As IDictionary, equalityComparer As IEqualityComparer)

參數

d
IDictionary

要複製到新 IDictionary 物件的 Hashtable 物件。The IDictionary object to copy to a new Hashtable object.

equalityComparer
IEqualityComparer

IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

-或--or- null 表示使用預設雜湊程式碼提供者和預設比較子。null to use the default hash code provider and the default comparer. 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

例外狀況

dnulld is null.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create the dictionary.
        SortedList^ mySL = gcnew SortedList();
        mySL->Add("FIRST", "Hello");
        mySL->Add("SECOND", "World");
        mySL->Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
    }
};

int main()
{
    SamplesHashtable::Main();
};

/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        var mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        var myHT2 = new Hashtable(mySL, new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
    }
}


/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL)

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

初始容量會設定為來源字典中的元素數目。The initial capacity is set to the number of elements in the source dictionary. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

IEqualityComparer 物件同時包含雜湊碼提供者和比較子。The IEqualityComparer object includes both the hash code provider and the comparer. 如果在 Hashtable 的函式中使用了 IEqualityComparer,在 Hashtable 物件中當做索引鍵使用的物件,就不需要覆寫 Object.GetHashCodeObject.Equals 方法。If an IEqualityComparer is used in the Hashtable constructor, the objects used as keys in the Hashtable object are not required to override the Object.GetHashCode and Object.Equals methods.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

IEqualityComparer 可以啟用使用不區分大小寫字串進行查閱的案例。The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Hashtable 的元素會以列舉值逐一查看 IDictionary 物件的相同順序排序。The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

此函式是 O(n) 運算,其中 nd 參數中的元素數目。This constructor is an O(n) operation, where n is the number of elements in the d parameter.

另請參閱

Hashtable(Int32)

使用指定的初始容量以及預設的載入因數、雜湊程式碼提供者和比較子,初始化 Hashtable 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity, and the default load factor, hash code provider, and comparer.

public:
 Hashtable(int capacity);
public Hashtable (int capacity);
new System.Collections.Hashtable : int -> System.Collections.Hashtable
Public Sub New (capacity As Integer)

參數

capacity
Int32

Hashtable 物件一開始可包含項目的大約數目。The approximate number of elements that the Hashtable object can initially contain.

例外狀況

capacity 小於零。capacity is less than zero.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(3)
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(3, New myCultureComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

指定初始容量,就不需要在將元素加入至 Hashtable 物件時執行數個調整大小作業。Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

此函式是 O(n) 作業,其中 ncapacityThis constructor is an O(n) operation, where n is capacity.

另請參閱

Hashtable(IEqualityComparer)

使用預設的初始容量和載入因數,以及指定的 Hashtable 物件,初始化 IEqualityComparer 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the default initial capacity and load factor, and the specified IEqualityComparer object.

public:
 Hashtable(System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (System.Collections.IEqualityComparer equalityComparer);
new System.Collections.Hashtable : System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (equalityComparer As IEqualityComparer)

參數

equalityComparer
IEqualityComparer

IEqualityComparer 物件,它會定義與 Hashtable 物件並用的雜湊程式碼提供者和比較子。The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable object.

-或--or- null 表示使用預設雜湊程式碼提供者和預設比較子。null to use the default hash code provider and the default comparer. 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myComparer : IEqualityComparer
{
public:
    virtual bool Equals(Object^ x, Object^ y) 
    {
        return x->Equals(y);
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

ref class myCultureComparer : IEqualityComparer
{
private:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

    virtual bool Equals(Object^ x, Object^ y) 
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    virtual int GetHashCode(Object^ obj)
    {
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

int main()
{
   
   // Create a hash table using the default hash code provider and the default comparer.
   Hashtable^ myHT1 = gcnew Hashtable((IEqualityComparer^)nullptr);
   myHT1->Add( "FIRST", "Hello" );
   myHT1->Add( "SECOND", "World" );
   myHT1->Add( "THIRD", "!" );
   
   // Create a hash table using the specified IEqualityComparer that uses
   // the default Object.Equals to determine equality.
   Hashtable^ myHT2 = gcnew Hashtable(gcnew myComparer());
   myHT2->Add( "FIRST", "Hello" );
   myHT2->Add( "SECOND", "World" );
   myHT2->Add( "THIRD", "!" );
   
   // Create a hash table using a case-insensitive hash code provider and
   // case-insensitive comparer based on the InvariantCulture.
   Hashtable^ myHT3 = gcnew Hashtable(
            CaseInsensitiveHashCodeProvider::DefaultInvariant,
            CaseInsensitiveComparer::DefaultInvariant);
   myHT3->Add( "FIRST", "Hello" );
   myHT3->Add( "SECOND", "World" );
   myHT3->Add( "THIRD", "!" );
   
   // Create a hash table using an IEqualityComparer that is based on
   // the Turkish culture (tr-TR) where "I" is not the uppercase
   // version of "i".
   CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
   Hashtable^ myHT4 = gcnew Hashtable( gcnew myCultureComparer(myCul) );
   myHT4->Add( "FIRST", "Hello" );
   myHT4->Add( "SECOND", "World" );
   myHT4->Add( "THIRD", "!" );
   
   // Search for a key in each hash table.
   Console::WriteLine( "first is in myHT1: {0}", myHT1->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT2: {0}", myHT2->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT3: {0}", myHT3->ContainsKey( "first" ) );
   Console::WriteLine( "first is in myHT4: {0}", myHT4->ContainsKey( "first" ) );
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/
using System;
using System.Collections;
using System.Globalization;

class myComparer : IEqualityComparer
{
    public new bool Equals(object x, object y)
    {
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable();
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the default Object.Equals to determine equality.
        var myHT2 = new Hashtable(new myComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using a case-insensitive hash code provider and
        // case-insensitive comparer based on the InvariantCulture.
        Hashtable myHT3 = new Hashtable(
            CaseInsensitiveHashCodeProvider.DefaultInvariant,
            CaseInsensitiveComparer.DefaultInvariant);
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");
        myHT4.Add("SECOND", "World");
        myHT4.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}");
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: False
first is in myHT3: True
first is in myHT4: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myComparer
    Implements IEqualityComparer
    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return x.Equals(y)
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode

        Return obj.ToString().ToLower().GetHashCode()
    End Function

End Class

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable

    Public Shared Sub Main()

        ' Create a hash table using the default comparer.
        Dim myHT1 As New Hashtable()
        myHT1.Add("FIRST", "Hello")
        myHT1.Add("SECOND", "World")
        myHT1.Add("THIRD", "!")

        ' Create a hash table using the specified IEqualityComparer that uses
        ' the default Object.Equals to determine equality.
        Dim myHT2 As New Hashtable(New myComparer())
        myHT2.Add("FIRST", "Hello")
        myHT2.Add("SECOND", "World")
        myHT2.Add("THIRD", "!")

        ' Create a hash table using a case-insensitive hash code provider and
        ' case-insensitive comparer based on the InvariantCulture.
        Dim myHT3 As New Hashtable( _
            CaseInsensitiveHashCodeProvider.DefaultInvariant, _
            CaseInsensitiveComparer.DefaultInvariant)
        myHT3.Add("FIRST", "Hello")
        myHT3.Add("SECOND", "World")
        myHT3.Add("THIRD", "!")

        ' Create a hash table using an IEqualityComparer that is based on
        ' the Turkish culture (tr-TR) where "I" is not the uppercase
        ' version of "i".
        Dim myCul As New CultureInfo("tr-TR")
        Dim myHT4 As New Hashtable(New myCultureComparer(myCul))
        myHT4.Add("FIRST", "Hello")
        myHT4.Add("SECOND", "World")
        myHT4.Add("THIRD", "!")

        ' Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")
        Console.WriteLine($"first is in myHT4: {myHT4.ContainsKey("first")}")

    End Sub

End Class

'This code produces the following output.
'Results vary depending on the system's culture settings.

'first is in myHT1: False
'first is in myHT2: False
'first is in myHT3: True
'first is in myHT4: False

備註

雜湊表的容量可用來根據負載因數來計算雜湊表值區的最佳數目。A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. 容量會視需要自動增加。Capacity is automatically increased as required.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

IEqualityComparer 物件同時包含雜湊碼提供者和比較子。The IEqualityComparer object includes both the hash code provider and the comparer. 如果在 Hashtable 的函式中使用了 IEqualityComparer,在 Hashtable 物件中當做索引鍵使用的物件,就不需要覆寫 Object.GetHashCodeObject.Equals 方法。If an IEqualityComparer is used in the Hashtable constructor, the objects used as keys in the Hashtable object are not required to override the Object.GetHashCode and Object.Equals methods.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

IEqualityComparer 可以啟用使用不區分大小寫字串進行查閱的案例。The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

此函式是 O(1) 作業。This constructor is an O(1) operation.

另請參閱

Hashtable(IDictionary)

藉由將指定字典中的項目複製到新的 Hashtable 物件,初始化 Hashtable 類別的新執行個體。Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Hashtable 物件的初始容量等於複製的項目數,並且此物件使用預設載入因數、雜湊程式碼提供者和比較子。The new Hashtable object has an initial capacity equal to the number of elements copied, and uses the default load factor, hash code provider, and comparer.

public:
 Hashtable(System::Collections::IDictionary ^ d);
public Hashtable (System.Collections.IDictionary d);
new System.Collections.Hashtable : System.Collections.IDictionary -> System.Collections.Hashtable
Public Sub New (d As IDictionary)

參數

d
IDictionary

要複製到新 IDictionary 物件的 Hashtable 物件。The IDictionary object to copy to a new Hashtable object.

例外狀況

dnulld is null.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create the dictionary.
        SortedList^ mySL = gcnew SortedList();
        mySL->Add("FIRST", "Hello");
        mySL->Add("SECOND", "World");
        mySL->Add("THIRD", "!");

        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(mySL, gcnew myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(mySL, gcnew myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));
    }
};

int main()
{
    SamplesHashtable::Main();
};

/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        return myComparer.Compare(x, y) == 0;
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create the dictionary.
        var mySL = new SortedList();
        mySL.Add("FIRST", "Hello");
        mySL.Add("SECOND", "World");
        mySL.Add("THIRD", "!");

        // Create a hash table using the default comparer.
        var myHT1 = new Hashtable(mySL);

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        var myHT2 = new Hashtable(mySL, new myCultureComparer());

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        var myCul = new CultureInfo("tr-TR");
        var myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

        // Search for a key in each hash table.
        Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}");
        Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}");
    }
}


/* 
This code produces the following output. 
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        Return myComparer.Compare(x, y) = 0
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create the dictionary.
      Dim mySL As New SortedList()
      mySL.Add("FIRST", "Hello")
      mySL.Add("SECOND", "World")
      mySL.Add("THIRD", "!")

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(mySL)

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(mySL, New myCultureComparer())

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(mySL, New myCultureComparer(myCul))

      ' Search for a key in each hash table.
      Console.WriteLine($"first is in myHT1: {myHT1.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT2: {myHT2.ContainsKey("first")}")
      Console.WriteLine($"first is in myHT3: {myHT3.ContainsKey("first")}")

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

初始容量會設定為來源字典中的元素數目。The initial capacity is set to the number of elements in the source dictionary. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

雜湊程式碼提供者會不表達 Hashtable 物件中索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable object. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

Hashtable 的元素會以列舉值逐一查看 IDictionary 物件的相同順序排序。The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

此函式是 O(n) 運算,其中 nd 參數中的元素數目。This constructor is an O(n) operation, where n is the number of elements in the d parameter.

另請參閱

Hashtable(Int32, IEqualityComparer)

使用指定的初始容量和 Hashtable,以及預設的載入因數,初始化 IEqualityComparer 類別的空白新執行個體。Initializes a new, empty instance of the Hashtable class using the specified initial capacity and IEqualityComparer, and the default load factor.

public:
 Hashtable(int capacity, System::Collections::IEqualityComparer ^ equalityComparer);
public Hashtable (int capacity, System.Collections.IEqualityComparer equalityComparer);
new System.Collections.Hashtable : int * System.Collections.IEqualityComparer -> System.Collections.Hashtable
Public Sub New (capacity As Integer, equalityComparer As IEqualityComparer)

參數

capacity
Int32

Hashtable 物件一開始可包含項目的大約數目。The approximate number of elements that the Hashtable object can initially contain.

equalityComparer
IEqualityComparer

IEqualityComparer 物件,它會定義與 Hashtable 並用的雜湊程式碼提供者和比較子。The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

-或--or- null 表示使用預設雜湊程式碼提供者和預設比較子。null to use the default hash code provider and the default comparer. 預設雜湊程式碼提供者是各金鑰的 GetHashCode() 實作,而預設比較子是各金鑰的 Equals(Object) 實作。The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

例外狀況

capacity 小於零。capacity is less than zero.

範例

下列程式碼範例會使用不同的 Hashtable 的函式建立雜湊表,並示範雜湊表的行為差異,即使每個資料表都包含相同的專案也一樣。The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables, even if each one contains the same elements.

using namespace System;
using namespace System::Collections;
using namespace System::Globalization;

ref class myCultureComparer : public IEqualityComparer
{
public:
    CaseInsensitiveComparer^ myComparer;

public:
    myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer::DefaultInvariant;
    }

public:
    myCultureComparer(CultureInfo^ myCulture)
    {
        myComparer = gcnew CaseInsensitiveComparer(myCulture);
    }

public:
    virtual bool Equals(Object^ x, Object^ y)
    {
        if (myComparer->Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

public:
    virtual int GetHashCode(Object^ obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj->ToString()->ToLower()->GetHashCode();
    }
};

public ref class SamplesHashtable
{

public:
    static void Main()
    {
        // Create a hash table using the default comparer.
        Hashtable^ myHT1 = gcnew Hashtable(3);
        myHT1->Add("FIRST", "Hello");
        myHT1->Add("SECOND", "World");
        myHT1->Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable^ myHT2 = gcnew Hashtable(3, gcnew myCultureComparer());
        myHT2->Add("FIRST", "Hello");
        myHT2->Add("SECOND", "World");
        myHT2->Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
        Hashtable^ myHT3 = gcnew Hashtable(3, gcnew myCultureComparer(myCul));
        myHT3->Add("FIRST", "Hello");
        myHT3->Add("SECOND", "World");
        myHT3->Add("THIRD", "!");

        // Search for a key in each hash table.
        Console::WriteLine("first is in myHT1: {0}", myHT1->ContainsKey("first"));
        Console::WriteLine("first is in myHT2: {0}", myHT2->ContainsKey("first"));
        Console::WriteLine("first is in myHT3: {0}", myHT3->ContainsKey("first"));

    }
};

int main()
{
    SamplesHashtable::Main();
}

/* 
This code produces the following output.  Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/
using System;
using System.Collections;
using System.Globalization;

class myCultureComparer : IEqualityComparer
{
    public CaseInsensitiveComparer myComparer;

    public myCultureComparer()
    {
        myComparer = CaseInsensitiveComparer.DefaultInvariant;
    }

    public myCultureComparer(CultureInfo myCulture)
    {
        myComparer = new CaseInsensitiveComparer(myCulture);
    }

    public new bool Equals(object x, object y)
    {
        if (myComparer.Compare(x, y) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(object obj)
    {
        // Compare the hash code for the lowercase versions of the strings.
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {

        // Create a hash table using the default comparer.
        Hashtable myHT1 = new Hashtable(3);
        myHT1.Add("FIRST", "Hello");
        myHT1.Add("SECOND", "World");
        myHT1.Add("THIRD", "!");

        // Create a hash table using the specified IEqualityComparer that uses
        // the CaseInsensitiveComparer.DefaultInvariant to determine equality.
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");

        // Create a hash table using an IEqualityComparer that is based on
        // the Turkish culture (tr-TR) where "I" is not the uppercase
        // version of "i".
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, new myCultureComparer(myCul));
        myHT3.Add("FIRST", "Hello");
        myHT3.Add("SECOND", "World");
        myHT3.Add("THIRD", "!");

        // Search for a key in each hash table.
        Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"));
        Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"));
        Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"));
    }
}


/* 
This code produces the following output.
Results vary depending on the system's culture settings.

first is in myHT1: False
first is in myHT2: True
first is in myHT3: False

*/

Imports System.Collections
Imports System.Globalization

Public Class myCultureComparer
    Implements IEqualityComparer

    Dim myComparer As CaseInsensitiveComparer

    Public Sub New()
        myComparer = CaseInsensitiveComparer.DefaultInvariant
    End Sub

    Public Sub New(ByVal myCulture As CultureInfo)
        myComparer = New CaseInsensitiveComparer(myCulture)
    End Sub

    Public Function Equals1(ByVal x As Object, ByVal y As Object) _
        As Boolean Implements IEqualityComparer.Equals

        If (myComparer.Compare(x, y) = 0) Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function GetHashCode1(ByVal obj As Object) _
        As Integer Implements IEqualityComparer.GetHashCode
        Return obj.ToString().ToLower().GetHashCode()
    End Function
End Class

Public Class SamplesHashtable   

   Public Shared Sub Main()

      ' Create a hash table using the default comparer.
      Dim myHT1 As New Hashtable(3)
      myHT1.Add("FIRST", "Hello")
      myHT1.Add("SECOND", "World")
      myHT1.Add("THIRD", "!")

      ' Create a hash table using the specified IEqualityComparer that uses
      ' the CaseInsensitiveComparer.DefaultInvariant to determine equality.
      Dim myHT2 As New Hashtable(3, New myCultureComparer())
      myHT2.Add("FIRST", "Hello")
      myHT2.Add("SECOND", "World")
      myHT2.Add("THIRD", "!")

      ' Create a hash table using an IEqualityComparer that is based on
      ' the Turkish culture (tr-TR) where "I" is not the uppercase
      ' version of "i".
      Dim myCul As New CultureInfo("tr-TR")
      Dim myHT3 As New Hashtable(3, New myCultureComparer(myCul))
      myHT3.Add("FIRST", "Hello")
      myHT3.Add("SECOND", "World")
      myHT3.Add("THIRD", "!")

      ' Search for a key in each hash table.
      Console.WriteLine("first is in myHT1: {0}", myHT1.ContainsKey("first"))
      Console.WriteLine("first is in myHT2: {0}", myHT2.ContainsKey("first"))
      Console.WriteLine("first is in myHT3: {0}", myHT3.ContainsKey("first"))

   End Sub

End Class


'This code produces the following output.
'Results vary depending on the system's culture settings.
'
'first is in myHT1: False
'first is in myHT2: True
'first is in myHT3: False

備註

指定初始容量,就不需要在將元素加入至 Hashtable 物件時執行數個調整大小作業。Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. 容量會視負載因素自動增加,視需要而定。Capacity is automatically increased as required based on the load factor.

負載因數是元素與值區的最大比例。The load factor is the maximum ratio of elements to buckets. 較小的載入因數表示以增加的記憶體耗用量成本來查詢的速度更快。A smaller load factor means faster lookup at the cost of increased memory consumption.

當實際的負載因數達到指定的載入因數時,值區的數目會自動增加到大於目前值區數目兩倍的最小質數。When the actual load factor reaches the specified load factor, the number of buckets is automatically increased to the smallest prime number that is larger than twice the current number of buckets.

IEqualityComparer 物件同時包含雜湊碼提供者和比較子。The IEqualityComparer object includes both the hash code provider and the comparer. 如果 Hashtable 的函式中使用了 IEqualityComparer,就不需要在 Hashtable 中當做索引鍵使用的物件來覆寫 Object.GetHashCodeObject.Equals 方法。If an IEqualityComparer is used in the Hashtable constructor, the objects used as keys in the Hashtable are not required to override the Object.GetHashCode and Object.Equals methods.

雜湊程式碼提供者會在 Hashtable中不表達索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the Hashtable. 預設雜湊程式碼提供者是 Object.GetHashCode的索引鍵的執行。The default hash code provider is the key's implementation of Object.GetHashCode.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. Hashtable 中的每個索引鍵都必須是唯一的。Every key in a Hashtable must be unique. 預設比較子是 Object.Equals的索引鍵的執行。The default comparer is the key's implementation of Object.Equals.

IEqualityComparer 可以啟用使用不區分大小寫字串進行查閱的案例。The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

此函式是 O(n) 運算,其中 ncapacity 參數。This constructor is an O(n) operation, where n is the capacity parameter.

另請參閱

適用於