Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

EqualityComparer<T>.Default Property

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> 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.
 * 
 */

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.