C# Hashtable Hashtable(IDictionary)

Description

Hashtable Hashtable(IDictionary) initializes a new instance of the Hashtable class by copying the elements from the specified dictionary to the new Hashtable object. 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.

Syntax

Hashtable.Hashtable(IDictionary) has the following syntax.


public Hashtable(
  IDictionary d
)

Parameters

Hashtable.Hashtable(IDictionary) has the following parameters.

  • d - The IDictionary object to copy to a new Hashtable object.

Example

The following code example creates hash tables using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables.


/*  www  .  j  av a2s.  c om*/
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)
    {
        return obj.ToString().ToLower().GetHashCode();
    }
}

public class SamplesHashtable
{

    public static void Main()
    {
        SortedList mySL = new SortedList();
        mySL.Add("FIRST", "Hello");

        Hashtable myHT1 = new Hashtable(mySL);

        Hashtable myHT2 = new Hashtable(mySL, new myCultureComparer());

        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(mySL, new myCultureComparer(myCul));

    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack