英語で読む

次の方法で共有


EqualityComparer<T>.Default プロパティ

定義

汎用引数で指定された型に対して、等値であるかどうかを比較するための既定の比較子を返します。

public static System.Collections.Generic.EqualityComparer<T> Default { get; }

プロパティ値

T 型の EqualityComparer<T> クラスの既定のインスタンス。

次の例では、型の要素を含むコレクションを作成し、 メソッドを Box 2 回呼び出して、別のボックスに一致するボックスを FindFirst 検索します。

最初の検索では等値比較子は指定されません。つまり FindFirst 、 を使用 EqualityComparer<T>.Default してボックスの等価性を判断します。 次に、 クラスの メソッドの IEquatable<T>.Equals 実装を Box 使用します。 寸法が同じ場合、2 つのボックスは等しいと見なされます。

2 番目の検索では、ボリュームごとの等価性を定義する等値比較子 (BoxEqVolume) を指定します。 ボリュームが同じ場合、2 つのボックスは等しいと見なされます。

using System;
using System.Collections.Generic;

static class Program
{
    static void Main()
    {
        var redBox = new Box(8, 8, 4);
        var blueBox = new Box(6, 8, 4);
        var greenBox = new Box(4, 8, 8);

        var boxes = new[] { redBox, blueBox, greenBox };

        var boxToFind = new Box(4, 8, 8);

        var foundByDimension = boxes.FindFirst(boxToFind);

        Console.WriteLine($"Found box {foundByDimension} by dimension.");

        var foundByVolume = boxes.FindFirst(boxToFind, new BoxEqVolume());

        Console.WriteLine($"Found box {foundByVolume} by volume.");
    }
}

public static class CollectionExtensions
{
    public static T FindFirst<T>(
        this IEnumerable<T> collection, T itemToFind, IEqualityComparer<T> comparer = null)
    {
        comparer = comparer ?? EqualityComparer<T>.Default;

        foreach (var item in collection)
        {
            if (comparer.Equals(item, itemToFind))
            {
                return item;
            }
        }

        throw new InvalidOperationException("No matching item found.");
    }
}

public class BoxEqVolume : EqualityComparer<Box>
{
    public override bool Equals(Box b1, Box b2)
    {
        if (object.ReferenceEquals(b1, b2))
            return true;

        if (b1 is null || b2 is null)
            return false;

        return b1.Volume == b2.Volume;
    }

    public override int GetHashCode(Box box) => box.Volume.GetHashCode();
}

public class Box : IEquatable<Box>
{
    public Box(int height, int length, int width)
    {
        this.Height = height;
        this.Length = length;
        this.Width = width;
    }

    public int Height { get; }
    public int Length { get; }
    public int Width { get; }

    public int Volume => Height * Length * Width;

    public bool Equals(Box other)
    {
        if (other is null)
            return false;

        return this.Height == other.Height && this.Length == other.Length
            && this.Width == other.Width;
    }

    public override bool Equals(object obj) => Equals(obj as Box);
    public override int GetHashCode() => (Height, Length, Width).GetHashCode();

    public override string ToString() => $"{Height} x {Length} x {Width}";
}

/* This example produces the following output:
 *
    Found box 4 x 8 x 8 by dimension.
    Found box 8 x 8 x 4 by volume.
 */

注釈

プロパティは Default 、型 T が インターフェイスを System.IEquatable<T> 実装しているかどうかを確認し、実装されている場合は、その実装を使用する を EqualityComparer<T> 返します。 それ以外の場合は、 によってT提供される および Object.GetHashCodeObject.Equalsオーバーライドを使用する を返EqualityComparer<T>します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください