C# Hashtable Hashtable(Int32, IEqualityComparer)

Description

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

Syntax

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


public Hashtable(
  int capacity,
  IEqualityComparer equalityComparer
)

Parameters

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

  • capacity - The approximate number of elements that the Hashtable object can initially contain.
  • 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

The following code example creates hash tables.


/*from w w  w  .ja v  a  2 s  .  co m*/

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()
    {
        Hashtable myHT2 = new Hashtable(3, new myCultureComparer());
        myHT2.Add("FIRST", "Hello");
        myHT2.Add("SECOND", "World");
        myHT2.Add("THIRD", "!");
    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack