C# Hashtable Hashtable(Int32, Single, IEqualityComparer)

Description

Hashtable Hashtable(Int32, Single, IEqualityComparer) initializes a new, empty instance of the Hashtable class using the specified initial capacity, load factor, and IEqualityComparer object.

Syntax

Hashtable.Hashtable(Int32, Single, IEqualityComparer) has the following syntax.


public Hashtable(
  int capacity,/* w  w  w  .j a  v  a  2  s.c  o m*/
  float loadFactor,
  IEqualityComparer equalityComparer
)

Parameters

Hashtable.Hashtable(Int32, Single, IEqualityComparer) has the following parameters.

  • capacity - The approximate number of elements that the Hashtable object can initially contain.
  • loadFactor - A number in the range from 0.1 through 1.0 that is multiplied by the default value which provides the best performance. The result is the maximum ratio of elements to buckets.
  • equalityComparer - The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable.
  • equalityComparer - -or-
  • equalityComparer - null to use the default hash code provider and the default comparer. The default hash code provider is each key's implementation of Object.GetHashCode and the default comparer is each key's implementation of Object.Equals.

Example


using System;//  w  ww . j a va  2  s . c  o m
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()
    {
        Hashtable myHT2 = new Hashtable(3, (float).8, new myCultureComparer());

        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT3 = new Hashtable(3, (float).8,new myCultureComparer(myCul));
    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack