EqualityComparer<T>.Default Property

Updated: January 2010

Returns a default equality comparer for the type specified by the generic argument.

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)

public static EqualityComparer<T> Default { get; }

Property Value

Type: System.Collections.Generic.EqualityComparer<T>
The default instance of the EqualityComparer<T> class for type T.

The Default property checks whether type T implements the System.IEquatable<T> generic interface and if so returns an EqualityComparer<T> that uses that implementation. Otherwise it returns an EqualityComparer<T> that uses the overrides of Object.Equals and Object.GetHashCode provided by T.

The following example creates two dictionary collections of type Box. Both dictionaries are created with the constructor that takes an EqualityComparer<T> as a parameter.

To determine the equality of dictionary keys, the boxesByDim dictionary uses an equality comparer (BoxEqDimensions) that uses the implementation of the IEquatable<T>.Equals method in the Box class. Two boxes are considered equal if their dimensions are the same.

The boxesByVol dictionary uses an equality comparer (BoxEqVolume) that defines equality by volume. Two boxes are considered equal if their volumes are the same.

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        BoxEqDimensions bxd = new BoxEqDimensions();
        BoxEqVolume bxv = new BoxEqVolume();

        Dictionary<Box, string> boxesByDim = new Dictionary<Box, string>(bxd);
        Dictionary<Box, string> boxesByVol = new Dictionary<Box, string>(bxv);

        try
        {
            Box redBox = new Box(8, 8, 4);
            Box blueBox = new Box(6, 8, 4);
            Box greenBox = new Box(4, 8, 8);

            Console.WriteLine("Adding boxes by Dimension");

            boxesByDim.Add(redBox, "red");
            boxesByDim.Add(blueBox, "blue");
            boxesByDim.Add(greenBox, "green");

            PrintBoxCollection(boxesByDim);

            Console.WriteLine("\nAdding boxes by Volume");

            boxesByVol.Add(redBox, "red");
            boxesByVol.Add(blueBox, "blue");
            boxesByVol.Add(greenBox, "green");

            PrintBoxCollection(boxesByVol);
        }
        catch (ArgumentException argEx)
        {
            Console.WriteLine(argEx.Message);
        }
    }
    private static void PrintBoxCollection(Dictionary<Box,string> boxes)
    {
        foreach (KeyValuePair<Box, string> kvp in boxes)
        {
            Console.WriteLine("{0} x {1} x {2} - {3}", kvp.Key.Height.ToString(),
                                                       kvp.Key.Length.ToString(),
                                                       kvp.Key.Width.ToString(), kvp.Value);
        }
    }
}

public class BoxEqDimensions : EqualityComparer<Box>
{
    public override int GetHashCode(Box bx)
    {
        int hCode = bx.Height ^ bx.Length ^ bx.Width;
        return hCode.GetHashCode();
    }

    public override bool Equals(Box b1, Box b2)
    {
        return EqualityComparer<Box>.Default.Equals(b1, b2);
    }
}


public class BoxEqVolume : EqualityComparer<Box>
{
    public override int GetHashCode(Box bx)
    {
        int hCode = bx.Height ^ bx.Length ^ bx.Width;
        return hCode.GetHashCode();
    }

    public override bool Equals(Box b1, Box b2)
    {
        if (b1.Height * b1.Width * b1.Length ==
            b2.Height * b2.Width * b2.Length)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public class Box : IEquatable<Box>
{

    public Box(int h, int l, int w)
    {
        this.Height = h;
        this.Length = l;
        this.Width = w;
    }
    public int Height { get; set; }
    public int Length { get; set; }
    public int Width { get; set; }

    public bool Equals(Box other)
    {
        if (this.Height == other.Height & this.Length == other.Length
            & this.Width == other.Width)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

/* This example produces the following output:
 * 
   Adding boxes by Dimension
    8 x 8 x 4 - red
    6 x 8 x 4 - blue
    4 x 8 x 8 - green

    Adding boxes by Volume
    An item with the same key has already been added.
 * 
 */

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

January 2010

Added example.

Information enhancement.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft