Default Property
Collapse the table of content
Expand the table of content

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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft