C# Hashtable Hashtable(IEqualityComparer)

Description

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

Syntax

Hashtable.Hashtable(IEqualityComparer) has the following syntax.


public Hashtable(
  IEqualityComparer equalityComparer
)

Parameters

Hashtable.Hashtable(IEqualityComparer) has the following parameters.

  • equalityComparer - The IEqualityComparer object that defines the hash code provider and the comparer to use with the Hashtable object.
  • 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 using different Hashtable constructors and demonstrates the differences in the behavior of the hash tables.


/*w  w w  .j av a2  s .  c o  m*/
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)
    {
        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()
    {
        CultureInfo myCul = new CultureInfo("tr-TR");
        Hashtable myHT4 = new Hashtable(new myCultureComparer(myCul));
        myHT4.Add("FIRST", "Hello");

    }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack