EqualityComparer<T> 類別

定義

提供基底類別 (Base Class) 用於 IEqualityComparer<T> 泛型介面的實作。

public abstract class EqualityComparer<T> : System.Collections.Generic.IEqualityComparer<T>, System.Collections.IEqualityComparer
[System.Serializable]
public abstract class EqualityComparer<T> : System.Collections.Generic.IEqualityComparer<T>, System.Collections.IEqualityComparer

類型參數

T

要比較之物件的類型。

繼承
EqualityComparer<T>
屬性
實作

範例

下列範例會建立具有相等比較子之 型 Box 別物件的字典集合。 如果兩個方塊的維度相同,則會視為相等。 然後,它會將方塊新增至集合。

字典會以相等比較子重新建立,以不同的方式定義相等:如果兩個方塊的磁碟區相同,則會視為相等。

using System;
using System.Collections.Generic;

class Program
{
    static Dictionary<Box, String> boxes;

    static void Main()
    {
        BoxSameDimensions boxDim = new BoxSameDimensions();
        boxes = new Dictionary<Box, string>(boxDim);

        Console.WriteLine("Boxes equality by dimensions:");
        Box redBox = new Box(8, 4, 8);
        Box greenBox = new Box(8, 6, 8);
        Box blueBox = new Box(8, 4, 8);
        Box yellowBox = new Box(8, 8, 8);
        AddBox(redBox, "red");
        AddBox(greenBox, "green");
        AddBox(blueBox, "blue");
        AddBox(yellowBox, "yellow");

        Console.WriteLine();
        Console.WriteLine("Boxes equality by volume:");

        BoxSameVolume boxVolume = new BoxSameVolume();
        boxes = new Dictionary<Box, string>(boxVolume);
        Box pinkBox = new Box(8, 4, 8);
        Box orangeBox = new Box(8, 6, 8);
        Box purpleBox = new Box(4, 8, 8);
        Box brownBox = new Box(8, 8, 4);
        AddBox(pinkBox, "pink");
        AddBox(orangeBox, "orange");
        AddBox(purpleBox, "purple");
        AddBox(brownBox, "brown");
    }

    public static void AddBox(Box bx, string name)
    {
        try
        {
            boxes.Add(bx, name);
            Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}",
                name, boxes.Count.ToString(), bx.GetHashCode());
        }
        catch (ArgumentException)
        {
            Console.WriteLine("A box equal to {0} is already in the collection.", name);
        }
    }
}

public class 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; }
}

class BoxSameDimensions : EqualityComparer<Box>
{
    public override bool Equals(Box b1, Box b2)
    {
        if (b1 == null && b2 == null)
            return true;
        else if (b1 == null || b2 == null)
            return false;

        return (b1.Height == b2.Height &&
                b1.Length == b2.Length &&
                b1.Width == b2.Width);
    }

    public override int GetHashCode(Box bx)
    {
        int hCode = bx.Height ^ bx.Length ^ bx.Width;
        return hCode.GetHashCode();
    }
}

class BoxSameVolume : EqualityComparer<Box>
{
    public override bool Equals(Box b1, Box b2)
    {
        if (b1 == null && b2 == null)
            return true;
        else if (b1 == null || b2 == null)
            return false;

        return (b1.Height * b1.Width * b1.Length ==
                b2.Height * b2.Width * b2.Length);
    }

    public override int GetHashCode(Box bx)
    {
        int hCode = bx.Height * bx.Length * bx.Width;
        return hCode.GetHashCode();
    }
}
/* This example produces an output similar to the following:
 *
      Boxes equality by dimensions:
      Added red, Count = 1, HashCode = 46104728
      Added green, Count = 2, HashCode = 12289376
      A box equal to blue is already in the collection.
      Added yellow, Count = 3, HashCode = 43495525

      Boxes equality by volume:
      Added pink, Count = 1, HashCode = 55915408
      Added orange, Count = 2, HashCode = 33476626
      A box equal to purple is already in the collection.
      A box equal to brown is already in the collection.
 *
*/

備註

衍生自這個類別,以提供泛型介面的 IEqualityComparer<T> 自定義實作,以便與泛型類別等 Dictionary<TKey,TValue> 集合類別搭配使用,或搭配 這類 List<T>.Sort方法使用。

屬性 Default 會檢查類型 T 是否實作 System.IEquatable<T> 泛型介面,如果是,則傳回 EqualityComparer<T> 叫用 方法實作的 IEquatable<T>.Equals 。 否則,它會傳回 EqualityComparer<T>,如 所 T提供。

在 .NET 8 和更新版本中,建議您使用 EqualityComparer<T>.Create(Func<T,T,Boolean>, Func<T,Int32>) 方法來建立此類型的實例。

建構函式

EqualityComparer<T>()

初始化 EqualityComparer<T> 類別的新執行個體。

屬性

Default

傳回泛型引數指定之類型的預設相等比較子 (Comparer)。

方法

Create(Func<T,T,Boolean>, Func<T,Int32>)

EqualityComparer<T>使用指定的委派來建立 ,做為比較子 和 GetHashCode(T) 方法的Equals(T, T)實作。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Equals(T, T)

在衍生類別中覆寫時,判斷型別為 T 的兩個物件是否相等。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetHashCode(T)

在衍生類別中覆寫時,做為雜湊演算法和資料結構之指定物件的雜湊函式,例如雜湊表。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IEqualityComparer.Equals(Object, Object)

判斷指定的物件是否相等。

IEqualityComparer.GetHashCode(Object)

傳回指定物件的雜湊碼。

適用於

另請參閱