Hashtable Konstruktoren

Definition

Initialisiert eine neue Instanz der Hashtable-Klasse.Initializes a new instance of the Hashtable class.

Überlädt

Hashtable()

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der anfänglichen Standardkapazität, des Standardlastfaktors, Hashcode-Standardanbieters und des Standardcomparers.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)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des angegebenen Lastfaktors, des angegebenen Hashcode-Anbieters und des angegebenen Comparers.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)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den angegebenen Lastfaktor, den angegebenen Hashcodeanbieter und den angegebenen Comparer.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)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des angegebenen Lastfaktors und des angegebenen IEqualityComparer-Objekts.Initializes a new, empty instance of the Hashtable class using the specified initial capacity, load factor, and IEqualityComparer object.

Hashtable(Int32, IHashCodeProvider, IComparer)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des angegebenen Hashcodeanbieters, des angegebenen Comparers und des Standardlastfaktors.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)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den angegebenen Lastfaktor und das angegebene IEqualityComparer-Objekt.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)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den Standardlastfaktor sowie den angegebenen Hashcodeanbieter und den angegebenen Comparer.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. Diese API ist veraltet.This API is obsolete. Eine Alternative finden Sie unter Hashtable(IDictionary, IEqualityComparer).For an alternative, see Hashtable(IDictionary, IEqualityComparer).

Hashtable(Int32, Single)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität und des Lastfaktors sowie des Hashcode-Standardanbieter und des angegebenen Comparers.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)

Initialisiert eine neue leere Instanz der Hashtable-Klasse, die serialisierbar ist, wobei die angegebenen SerializationInfo-Objekte und die angegebenen StreamingContext-Objekte verwendet werden.Initializes a new, empty instance of the Hashtable class that is serializable using the specified SerializationInfo and StreamingContext objects.

Hashtable(IHashCodeProvider, IComparer)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der anfänglichen Standardkapazität, des Standardlastfaktors, des angegebenen Hashcodeanbieters und des angegebenen Comparers.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)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den angegebenen Lastfaktor sowie den Hashcode-Standardanbieter und den Standardcomparer.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)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in ein neues Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to a new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den Standardlastfaktor sowie das angegebene IEqualityComparer-Objekt.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)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des Standardlastfaktors, des Hashcode-Standardanbieters und des Standardcomparers.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)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der anfänglichen Standardkapazität, des Standardlastfaktors und des angegebenen IEqualityComparer-Objekts.Initializes a new, empty instance of the Hashtable class using the default initial capacity and load factor, and the specified IEqualityComparer object.

Hashtable(IDictionary)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den Standardlastfaktor, den Hashcode-Standardanbieter und den Standardcomparer.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)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität und des angegebenen IEqualityComparer sowie des Standardlastfaktors.Initializes a new, empty instance of the Hashtable class using the specified initial capacity and IEqualityComparer, and the default load factor.

Hashtable()

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der anfänglichen Standardkapazität, des Standardlastfaktors, Hashcode-Standardanbieters und des Standardcomparers.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 ()

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die Kapazität einer Hash Tabelle wird verwendet, um die optimale Anzahl von Hash Tabellen-Bucket basierend auf dem Lastfaktor zu berechnen.A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. Die Kapazität wird bei Bedarf automatisch erweitert.Capacity is automatically increased as required.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Dieser Konstruktor ist ein O(1)-Vorgang.This constructor is an O(1) operation.

Siehe auch

Hashtable(Int32, Single, IHashCodeProvider, IComparer)

Warnung

Diese API ist jetzt veraltet.

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des angegebenen Lastfaktors, des angegebenen Hashcode-Anbieters und des angegebenen Comparers.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);
[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)

Parameter

capacity
Int32

Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.The approximate number of elements that the Hashtable object can initially contain.

loadFactor
Single

Eine Zahl im Bereich 0,1 bis 1,0, die mit dem Standardwert für die optimale Leistung multipliziert wird.A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.The result is the maximum ratio of elements to buckets.

hcp
IHashCodeProvider

Das IHashCodeProvider-Objekt, das die Hashcodes für alle Schlüssel in der Hashtable bereitstellt.The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

- oder --or- null, wenn der Hashcode-Standardanbieter verwendet werden soll. Dies ist die jeweilige GetHashCode()-Implementierung eines Schlüssels.null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

Das IComparer-Objekt, mit dem ermittelt wird, ob zwei Schlüssel gleich sind.The IComparer object to use to determine whether two keys are equal.

- oder --or- null, wenn der Standardcomparer verwendet werden soll. Dies ist die jeweilige Equals(Object)-Implementierung eines Schlüssels.null to use the default comparer, which is each key's implementation of Equals(Object).

Attribute

Ausnahmen

capacity ist kleiner als Null.capacity is less than zero.

- oder --or- loadFactor ist kleiner als 0,1.loadFactor is less than 0.1.

- oder --or- loadFactor ist größer als 1,0.loadFactor is greater than 1.0.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Wenn Sie die anfängliche Kapazität angeben, entfällt die Notwendigkeit, beim Hinzufügen von Elementen zum Hashtable-Objekt eine Reihe von Ändern der Größe auszuführen.Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption. Der Lastfaktor 1,0 ist das beste Gleichgewicht zwischen Geschwindigkeit und Größe.A load factor of 1.0 is the best balance between speed and size.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der benutzerdefinierte Hashcode-Anbieter und der benutzerdefinierte Vergleich ermöglichen Szenarien wie Suchvorgänge mit Zeichen folgen ohne Beachtung der Groß-und Kleinschreibung.The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n der capacity-Parameter ist.This constructor is an O(n) operation, where n is the capacity parameter.

Siehe auch

Hashtable(IDictionary, Single, IHashCodeProvider, IComparer)

Warnung

Diese API ist jetzt veraltet.

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den angegebenen Lastfaktor, den angegebenen Hashcodeanbieter und den angegebenen Comparer.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);
[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)

Parameter

d
IDictionary

Das IDictionary-Objekt, das in ein neues Hashtable-Objekt kopiert werden soll.The IDictionary object to copy to a new Hashtable object.

loadFactor
Single

Eine Zahl im Bereich 0.1 bis 1.0, die mit dem Standardwert für die optimale Leistung multipliziert wird.A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.The result is the maximum ratio of elements to buckets.

hcp
IHashCodeProvider

Das IHashCodeProvider-Objekt, das die Hashcodes für alle Schlüssel in der Hashtable bereitstellt.The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

- oder --or- null, wenn der Hashcode-Standardanbieter verwendet werden soll. Dies ist die jeweilige GetHashCode()-Implementierung eines Schlüssels.null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

Das IComparer-Objekt, mit dem ermittelt wird, ob zwei Schlüssel gleich sind.The IComparer object to use to determine whether two keys are equal.

- oder --or- null, wenn der Standardcomparer verwendet werden soll. Dies ist die jeweilige Equals(Object)-Implementierung eines Schlüssels.null to use the default comparer, which is each key's implementation of Equals(Object).

Attribute

Ausnahmen

d ist null.d is null.

loadFactor ist kleiner als 0,1.loadFactor is less than 0.1.

- oder --or- loadFactor ist größer als 1,0.loadFactor is greater than 1.0.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die anfängliche Kapazität wird auf die Anzahl der Elemente im Quell Wörterbuch festgelegt.The initial capacity is set to the number of elements in the source dictionary. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption. Der Lastfaktor 1,0 ist das beste Gleichgewicht zwischen Geschwindigkeit und Größe.A load factor of 1.0 is the best balance between speed and size.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der benutzerdefinierte Hashcode-Anbieter und der benutzerdefinierte Vergleich ermöglichen Szenarien wie Suchvorgänge mit Zeichen folgen ohne Beachtung der Groß-und Kleinschreibung.The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Die Elemente der neuen Hashtable werden in derselben Reihenfolge sortiert, in der der Enumerator das IDictionary-Objekt durchläuft.The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente im d-Parameter ist.This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Hashtable(Int32, Single, IEqualityComparer)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des angegebenen Lastfaktors und des angegebenen IEqualityComparer-Objekts.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)

Parameter

capacity
Int32

Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.The approximate number of elements that the Hashtable object can initially contain.

loadFactor
Single

Eine Zahl im Bereich 0,1 bis 1,0, die mit dem Standardwert für die optimale Leistung multipliziert wird.A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.The result is the maximum ratio of elements to buckets.

equalityComparer
IEqualityComparer

Der IEqualityComparer, der sowohl den Hashcodeanbieter als auch den Comparer zur Verwendung mit der Hashtable definiert.The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

- oder --or- null, um den Hashcode-Standardanbieter und den Standardcomparer zu verwenden.null to use the default hash code provider and the default comparer. Der Hashcode-Standardanbieter ist die Implementierung von GetHashCode() jedes Schlüssels, und der Standardcomparer ist die Implementierung von Equals(Object) jedes Schlüssels.The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

Ausnahmen

capacity ist kleiner als Null.capacity is less than zero.

- oder --or- loadFactor ist kleiner als 0,1.loadFactor is less than 0.1.

- oder --or- loadFactor ist größer als 1,0.loadFactor is greater than 1.0.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Wenn Sie die anfängliche Kapazität angeben, entfällt die Notwendigkeit, beim Hinzufügen von Elementen zum Hashtable-Objekt eine Reihe von Ändern der Größe auszuführen.Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption. Der Lastfaktor 1,0 ist das beste Gleichgewicht zwischen Geschwindigkeit und Größe.A load factor of 1.0 is the best balance between speed and size.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Das IEqualityComparer-Objekt enthält sowohl den Hash Code Anbieter als auch den Comparer.The IEqualityComparer object includes both the hash code provider and the comparer. Wenn ein IEqualityComparer im Hashtable-Konstruktor verwendet wird, müssen die-Objekte, die als Schlüssel in den Hashtable verwendet werden, die Object.GetHashCode-und Object.Equals-Methode nicht überschreiben.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der IEqualityComparer ermöglicht Szenarien wie das Durchsuchen von Such Vorgängen ohne Beachtung der Groß-/Kleinschreibung.The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n der capacity-Parameter ist.This constructor is an O(n) operation, where n is the capacity parameter.

Siehe auch

Hashtable(Int32, IHashCodeProvider, IComparer)

Warnung

Diese API ist jetzt veraltet.

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des angegebenen Hashcodeanbieters, des angegebenen Comparers und des Standardlastfaktors.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);
[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)

Parameter

capacity
Int32

Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.The approximate number of elements that the Hashtable object can initially contain.

hcp
IHashCodeProvider

Das IHashCodeProvider-Objekt, das die Hashcodes für alle Schlüssel in der Hashtable bereitstellt.The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

- oder --or- null, wenn der Hashcode-Standardanbieter verwendet werden soll. Dies ist die jeweilige GetHashCode()-Implementierung eines Schlüssels.null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

Das IComparer-Objekt, mit dem ermittelt wird, ob zwei Schlüssel gleich sind.The IComparer object to use to determine whether two keys are equal.

- oder --or- null, wenn der Standardcomparer verwendet werden soll. Dies ist die jeweilige Equals(Object)-Implementierung eines Schlüssels.null to use the default comparer, which is each key's implementation of Equals(Object).

Attribute

Ausnahmen

capacity ist kleiner als Null.capacity is less than zero.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Wenn Sie die anfängliche Kapazität angeben, entfällt die Notwendigkeit, beim Hinzufügen von Elementen zum Hashtable-Objekt eine Reihe von Ändern der Größe auszuführen.Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der benutzerdefinierte Hashcode-Anbieter und der benutzerdefinierte Vergleich ermöglichen Szenarien wie Suchvorgänge mit Zeichen folgen ohne Beachtung der Groß-und Kleinschreibung.The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n der capacity-Parameter ist.This constructor is an O(n) operation, where n is the capacity parameter.

Siehe auch

Hashtable(IDictionary, Single, IEqualityComparer)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den angegebenen Lastfaktor und das angegebene IEqualityComparer-Objekt.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)

Parameter

d
IDictionary

Das IDictionary-Objekt, das in ein neues Hashtable-Objekt kopiert werden soll.The IDictionary object to copy to a new Hashtable object.

loadFactor
Single

Eine Zahl im Bereich 0.1 bis 1.0, die mit dem Standardwert für die optimale Leistung multipliziert wird.A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.The result is the maximum ratio of elements to buckets.

equalityComparer
IEqualityComparer

Der IEqualityComparer, der sowohl den Hashcodeanbieter als auch den Comparer zur Verwendung mit der Hashtable definiert.The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

- oder --or- null, um den Hashcode-Standardanbieter und den Standardcomparer zu verwenden.null to use the default hash code provider and the default comparer. Der Hashcode-Standardanbieter ist die Implementierung von GetHashCode() jedes Schlüssels, und der Standardcomparer ist die Implementierung von Equals(Object) jedes Schlüssels.The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

Ausnahmen

d ist null.d is null.

loadFactor ist kleiner als 0,1.loadFactor is less than 0.1.

- oder --or- loadFactor ist größer als 1,0.loadFactor is greater than 1.0.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die anfängliche Kapazität wird auf die Anzahl der Elemente im Quell Wörterbuch festgelegt.The initial capacity is set to the number of elements in the source dictionary. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption. Der Lastfaktor 1,0 ist das beste Gleichgewicht zwischen Geschwindigkeit und Größe.A load factor of 1.0 is the best balance between speed and size.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Das IEqualityComparer-Objekt enthält sowohl den Hash Code Anbieter als auch den Comparer.The IEqualityComparer object includes both the hash code provider and the comparer. Wenn ein IEqualityComparer im Hashtable-Konstruktor verwendet wird, müssen die-Objekte, die als Schlüssel im Hashtable-Objekt verwendet werden, die Object.GetHashCode-und Object.Equals-Methode nicht überschreiben.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der IEqualityComparer ermöglicht Szenarien wie das Durchsuchen von Such Vorgängen ohne Beachtung der Groß-/Kleinschreibung.The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Die Elemente der neuen Hashtable werden in derselben Reihenfolge sortiert, in der der Enumerator das IDictionary-Objekt durchläuft.The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente im d-Parameter ist.This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Siehe auch

Hashtable(IDictionary, IHashCodeProvider, IComparer)

Warnung

Diese API ist jetzt veraltet.

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den Standardlastfaktor sowie den angegebenen Hashcodeanbieter und den angegebenen Comparer.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. Diese API ist veraltet.This API is obsolete. Eine Alternative finden Sie unter Hashtable(IDictionary, IEqualityComparer).For an alternative, see Hashtable(IDictionary, IEqualityComparer).

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)

Parameter

d
IDictionary

Das IDictionary-Objekt, das in ein neues Hashtable-Objekt kopiert werden soll.The IDictionary object to copy to a new Hashtable object.

hcp
IHashCodeProvider

Das IHashCodeProvider-Objekt, das die Hashcodes für alle Schlüssel in der Hashtable bereitstellt.The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable.

- oder --or- null, wenn der Hashcode-Standardanbieter verwendet werden soll. Dies ist die jeweilige GetHashCode()-Implementierung eines Schlüssels.null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

Das IComparer-Objekt, mit dem ermittelt wird, ob zwei Schlüssel gleich sind.The IComparer object to use to determine whether two keys are equal.

- oder --or- null, wenn der Standardcomparer verwendet werden soll. Dies ist die jeweilige Equals(Object)-Implementierung eines Schlüssels.null to use the default comparer, which is each key's implementation of Equals(Object).

Attribute

Ausnahmen

d ist null.d is null.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die anfängliche Kapazität wird auf die Anzahl der Elemente im Quell Wörterbuch festgelegt.The initial capacity is set to the number of elements in the source dictionary. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der benutzerdefinierte Hashcode-Anbieter und der benutzerdefinierte Vergleich ermöglichen Szenarien wie Suchvorgänge mit Zeichen folgen ohne Beachtung der Groß-und Kleinschreibung.The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Die Elemente der neuen Hashtable werden in derselben Reihenfolge sortiert, in der der Enumerator das IDictionary-Objekt durchläuft.The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente im d-Parameter ist.This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Siehe auch

Hashtable(Int32, Single)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität und des Lastfaktors sowie des Hashcode-Standardanbieter und des angegebenen Comparers.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)

Parameter

capacity
Int32

Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.The approximate number of elements that the Hashtable object can initially contain.

loadFactor
Single

Eine Zahl im Bereich 0,1 bis 1,0, die mit dem Standardwert für die optimale Leistung multipliziert wird.A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.The result is the maximum ratio of elements to buckets.

Ausnahmen

capacity ist kleiner als Null.capacity is less than zero.

- oder --or- loadFactor ist kleiner als 0,1.loadFactor is less than 0.1.

- oder --or- loadFactor ist größer als 1,0.loadFactor is greater than 1.0.

capacity verursacht einen Überlauf.capacity is causing an overflow.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Wenn Sie die anfängliche Kapazität angeben, entfällt die Notwendigkeit, beim Hinzufügen von Elementen zum Hashtable-Objekt eine Reihe von Ändern der Größe auszuführen.Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption. Der Lastfaktor 1,0 ist das beste Gleichgewicht zwischen Geschwindigkeit und Größe.A load factor of 1.0 is the best balance between speed and size.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n der capacity-Parameter ist.This constructor is an O(n) operation, where n is the capacity parameter.

Siehe auch

Hashtable(SerializationInfo, StreamingContext)

Initialisiert eine neue leere Instanz der Hashtable-Klasse, die serialisierbar ist, wobei die angegebenen SerializationInfo-Objekte und die angegebenen StreamingContext-Objekte verwendet werden.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)

Parameter

info
SerializationInfo

Ein SerializationInfo-Objekt mit den zum Serialisieren des Hashtable-Objekts erforderlichen Informationen.A SerializationInfo object containing the information required to serialize the Hashtable object.

context
StreamingContext

Ein StreamingContext-Objekt mit der Quelle und dem Ziel des entsprechenden serialisierten Streams für Hashtable.A StreamingContext object containing the source and destination of the serialized stream associated with the Hashtable.

Ausnahmen

info ist null.info is null.

Hinweise

Die Kapazität einer Hash Tabelle wird verwendet, um die optimale Anzahl von Hash Tabellen-Bucket basierend auf dem Lastfaktor zu berechnen.A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. Die Kapazität wird bei Bedarf automatisch erweitert.Capacity is automatically increased as required.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n Count ist.This constructor is an O(n) operation, where n is Count.

Da das Serialisieren und Deserialisieren eines Enumerators für eine Hashtable bewirken kann, dass die Elemente neu angeordnet werden, ist es nicht möglich, die Enumeration fortzusetzen, ohne die Reset-Methode aufzurufende.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.

Siehe auch

Hashtable(IHashCodeProvider, IComparer)

Warnung

Diese API ist jetzt veraltet.

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der anfänglichen Standardkapazität, des Standardlastfaktors, des angegebenen Hashcodeanbieters und des angegebenen Comparers.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);
[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)

Parameter

hcp
IHashCodeProvider

Das IHashCodeProvider-Objekt, das die Hashcodes für alle Schlüssel im Hashtable-Objekt bereitstellt.The IHashCodeProvider object that supplies the hash codes for all keys in the Hashtable object.

- oder --or- null, wenn der Hashcode-Standardanbieter verwendet werden soll. Dies ist die jeweilige GetHashCode()-Implementierung eines Schlüssels.null to use the default hash code provider, which is each key's implementation of GetHashCode().

comparer
IComparer

Das IComparer-Objekt, mit dem ermittelt wird, ob zwei Schlüssel gleich sind.The IComparer object to use to determine whether two keys are equal.

- oder --or- null, wenn der Standardcomparer verwendet werden soll. Dies ist die jeweilige Equals(Object)-Implementierung eines Schlüssels.null to use the default comparer, which is each key's implementation of Equals(Object).

Attribute

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die Kapazität einer Hash Tabelle wird verwendet, um die optimale Anzahl von Hash Tabellen-Bucket basierend auf dem Lastfaktor zu berechnen.A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. Die Kapazität wird bei Bedarf automatisch erweitert.Capacity is automatically increased as required.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der benutzerdefinierte Hashcode-Anbieter und der benutzerdefinierte Vergleich ermöglichen Szenarien wie Suchvorgänge mit Zeichen folgen ohne Beachtung der Groß-und Kleinschreibung.The custom hash code provider and the custom comparer enable scenarios such as doing lookups with case-insensitive strings.

Dieser Konstruktor ist ein O(1)-Vorgang.This constructor is an O(1) operation.

Siehe auch

Hashtable(IDictionary, Single)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den angegebenen Lastfaktor sowie den Hashcode-Standardanbieter und den Standardcomparer.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)

Parameter

d
IDictionary

Das IDictionary-Objekt, das in ein neues Hashtable-Objekt kopiert werden soll.The IDictionary object to copy to a new Hashtable object.

loadFactor
Single

Eine Zahl im Bereich 0.1 bis 1.0, die mit dem Standardwert für die optimale Leistung multipliziert wird.A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. Das Ergebnis ist das maximale Verhältnis von Elementen zu Buckets.The result is the maximum ratio of elements to buckets.

Ausnahmen

d ist null.d is null.

loadFactor ist kleiner als 0,1.loadFactor is less than 0.1.

- oder --or- loadFactor ist größer als 1,0.loadFactor is greater than 1.0.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die anfängliche Kapazität wird auf die Anzahl der Elemente im Quell Wörterbuch festgelegt.The initial capacity is set to the number of elements in the source dictionary. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption. Der Lastfaktor 1,0 ist das beste Gleichgewicht zwischen Geschwindigkeit und Größe.A load factor of 1.0 is the best balance between speed and size.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Die Elemente der neuen Hashtable werden in derselben Reihenfolge sortiert, in der der Enumerator das IDictionary-Objekt durchläuft.The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente im d-Parameter ist.This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Siehe auch

Hashtable(IDictionary, IEqualityComparer)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in ein neues Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to a new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den Standardlastfaktor sowie das angegebene IEqualityComparer-Objekt.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)

Parameter

d
IDictionary

Das IDictionary-Objekt, das in ein neues Hashtable-Objekt kopiert werden soll.The IDictionary object to copy to a new Hashtable object.

equalityComparer
IEqualityComparer

Der IEqualityComparer, der sowohl den Hashcodeanbieter als auch den Comparer zur Verwendung mit der Hashtable definiert.The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

- oder --or- null, um den Hashcode-Standardanbieter und den Standardcomparer zu verwenden.null to use the default hash code provider and the default comparer. Der Hashcode-Standardanbieter ist die Implementierung von GetHashCode() jedes Schlüssels, und der Standardcomparer ist die Implementierung von Equals(Object) jedes Schlüssels.The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

Ausnahmen

d ist null.d is null.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die anfängliche Kapazität wird auf die Anzahl der Elemente im Quell Wörterbuch festgelegt.The initial capacity is set to the number of elements in the source dictionary. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Das IEqualityComparer-Objekt enthält sowohl den Hash Code Anbieter als auch den Comparer.The IEqualityComparer object includes both the hash code provider and the comparer. Wenn ein IEqualityComparer im Hashtable-Konstruktor verwendet wird, müssen die-Objekte, die als Schlüssel im Hashtable-Objekt verwendet werden, die Object.GetHashCode-und Object.Equals-Methode nicht überschreiben.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der IEqualityComparer ermöglicht Szenarien wie das Durchsuchen von Such Vorgängen ohne Beachtung der Groß-/Kleinschreibung.The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Die Elemente der neuen Hashtable werden in derselben Reihenfolge sortiert, in der der Enumerator das IDictionary-Objekt durchläuft.The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente im d-Parameter ist.This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Siehe auch

Hashtable(Int32)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität, des Standardlastfaktors, des Hashcode-Standardanbieters und des Standardcomparers.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)

Parameter

capacity
Int32

Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.The approximate number of elements that the Hashtable object can initially contain.

Ausnahmen

capacity ist kleiner als Null.capacity is less than zero.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Wenn Sie die anfängliche Kapazität angeben, entfällt die Notwendigkeit, beim Hinzufügen von Elementen zum Hashtable-Objekt eine Reihe von Ändern der Größe auszuführen.Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n capacity ist.This constructor is an O(n) operation, where n is capacity.

Siehe auch

Hashtable(IEqualityComparer)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der anfänglichen Standardkapazität, des Standardlastfaktors und des angegebenen IEqualityComparer-Objekts.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)

Parameter

equalityComparer
IEqualityComparer

Das IEqualityComparer-Objekt, das sowohl den Hashcodeanbieter als auch den Comparer zur Verwendung mit der Hashtable definiert.The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable object.

- oder --or- null, um den Hashcode-Standardanbieter und den Standardcomparer zu verwenden.null to use the default hash code provider and the default comparer. Der Hashcode-Standardanbieter ist die Implementierung von GetHashCode() jedes Schlüssels, und der Standardcomparer ist die Implementierung von Equals(Object) jedes Schlüssels.The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die Kapazität einer Hash Tabelle wird verwendet, um die optimale Anzahl von Hash Tabellen-Bucket basierend auf dem Lastfaktor zu berechnen.A hash table's capacity is used to calculate the optimal number of hash table buckets based on the load factor. Die Kapazität wird bei Bedarf automatisch erweitert.Capacity is automatically increased as required.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Das IEqualityComparer-Objekt enthält sowohl den Hash Code Anbieter als auch den Comparer.The IEqualityComparer object includes both the hash code provider and the comparer. Wenn ein IEqualityComparer im Hashtable-Konstruktor verwendet wird, müssen die-Objekte, die als Schlüssel im Hashtable-Objekt verwendet werden, die Object.GetHashCode-und Object.Equals-Methode nicht überschreiben.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der IEqualityComparer ermöglicht Szenarien wie das Durchsuchen von Such Vorgängen ohne Beachtung der Groß-/Kleinschreibung.The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Dieser Konstruktor ist ein O(1)-Vorgang.This constructor is an O(1) operation.

Siehe auch

Hashtable(IDictionary)

Initialisiert eine neue Instanz der Hashtable-Klasse durch Kopieren der Elemente aus dem angegebenen Wörterbuch in das neue Hashtable-Objekt.Initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. Das neue Hashtable-Objekt besitzt eine Anfangskapazität, die mit der Anzahl der kopierten Elemente identisch ist, und verwendet den Standardlastfaktor, den Hashcode-Standardanbieter und den Standardcomparer.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)

Parameter

d
IDictionary

Das IDictionary-Objekt, das in ein neues Hashtable-Objekt kopiert werden soll.The IDictionary object to copy to a new Hashtable object.

Ausnahmen

d ist null.d is null.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Die anfängliche Kapazität wird auf die Anzahl der Elemente im Quell Wörterbuch festgelegt.The initial capacity is set to the number of elements in the source dictionary. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel im Hashtable-Objekt aus.The hash code provider dispenses hash codes for keys in the Hashtable object. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Die Elemente der neuen Hashtable werden in derselben Reihenfolge sortiert, in der der Enumerator das IDictionary-Objekt durchläuft.The elements of the new Hashtable are sorted in the same order in which the enumerator iterates through the IDictionary object.

Dieser Konstruktor ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente im d-Parameter ist.This constructor is an O(n) operation, where n is the number of elements in the d parameter.

Siehe auch

Hashtable(Int32, IEqualityComparer)

Initialisiert eine neue leere Instanz der Hashtable-Klasse unter Verwendung der angegebenen Anfangskapazität und des angegebenen IEqualityComparer sowie des Standardlastfaktors.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)

Parameter

capacity
Int32

Die ungefähre Anzahl von Elementen, die die Hashtable anfänglich enthalten kann.The approximate number of elements that the Hashtable object can initially contain.

equalityComparer
IEqualityComparer

Der IEqualityComparer, der sowohl den Hashcodeanbieter als auch den Comparer zur Verwendung mit der Hashtable definiert.The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.

- oder --or- null, um den Hashcode-Standardanbieter und den Standardcomparer zu verwenden.null to use the default hash code provider and the default comparer. Der Hashcode-Standardanbieter ist die Implementierung von GetHashCode() jedes Schlüssels, und der Standardcomparer ist die Implementierung von Equals(Object) jedes Schlüssels.The default hash code provider is each key's implementation of GetHashCode() and the default comparer is each key's implementation of Equals(Object).

Ausnahmen

capacity ist kleiner als Null.capacity is less than zero.

Beispiele

Im folgenden Codebeispiel werden Hash Tabellen mit unterschiedlichen Hashtable-Konstruktoren erstellt und die Unterschiede im Verhalten der Hash Tabellen veranschaulicht, auch wenn beide dieselben Elemente enthalten.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

Hinweise

Wenn Sie die anfängliche Kapazität angeben, entfällt die Notwendigkeit, beim Hinzufügen von Elementen zum Hashtable-Objekt eine Reihe von Ändern der Größe auszuführen.Specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Hashtable object. Die Kapazität wird basierend auf dem Ladefaktor automatisch nach Bedarf angepasst.Capacity is automatically increased as required based on the load factor.

Der Ladefaktor ist das maximale Verhältnis von Elementen zu Bucket.The load factor is the maximum ratio of elements to buckets. Ein geringerer Ladefaktor bedeutet einen schnelleren Such Faktor, um den Speicherverbrauch zu erhöhen.A smaller load factor means faster lookup at the cost of increased memory consumption.

Wenn der tatsächliche Lastfaktor den angegebenen Lastfaktor erreicht, wird die Bucketanzahl automatisch auf die kleinste Primzahl vergrößert, die größer ist als die doppelte Anzahl an bucketwerten.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.

Das IEqualityComparer-Objekt enthält sowohl den Hash Code Anbieter als auch den Comparer.The IEqualityComparer object includes both the hash code provider and the comparer. Wenn ein IEqualityComparer im Hashtable-Konstruktor verwendet wird, müssen die-Objekte, die als Schlüssel in den Hashtable verwendet werden, die Object.GetHashCode-und Object.Equals-Methode nicht überschreiben.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.

Der Hashcode-Anbieter gibt Hashcodes für Schlüssel in der Hashtable aus.The hash code provider dispenses hash codes for keys in the Hashtable. Der standardmäßige Hashcode-Anbieter ist die Implementierung des Schlüssels von Object.GetHashCode.The default hash code provider is the key's implementation of Object.GetHashCode.

Der Vergleich bestimmt, ob zwei Schlüssel gleich sind.The comparer determines whether two keys are equal. Jeder Schlüssel in einer Hashtable muss eindeutig sein.Every key in a Hashtable must be unique. Der Standardcomparer ist die Implementierung des Schlüssels von Object.Equals.The default comparer is the key's implementation of Object.Equals.

Der IEqualityComparer ermöglicht Szenarien wie das Durchsuchen von Such Vorgängen ohne Beachtung der Groß-/Kleinschreibung.The IEqualityComparer enables scenarios such as doing lookups with case-insensitive strings.

Dieser Konstruktor ist ein O(n)-Vorgang, bei dem n der capacity-Parameter ist.This constructor is an O(n) operation, where n is the capacity parameter.

Siehe auch

Gilt für: