EqualityComparer<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 IEqualityComparer<T> 泛型接口的实现提供基类。
generic <typename T>
public ref class EqualityComparer abstract : System::Collections::Generic::IEqualityComparer<T>, System::Collections::IEqualityComparer
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
type EqualityComparer<'T> = class
interface IEqualityComparer<'T>
interface IEqualityComparer
[<System.Serializable>]
type EqualityComparer<'T> = class
interface IEqualityComparer
interface IEqualityComparer<'T>
Public MustInherit Class EqualityComparer(Of T)
Implements IEqualityComparer, IEqualityComparer(Of T)
类型参数
- 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.
*
*/
'Imports System.Collections
Imports System.Collections.Generic
Module Program
Dim boxes As Dictionary(Of Box, [String])
Public Sub Main(ByVal args As String())
Dim boxDim As New BoxSameDimensions()
boxes = New Dictionary(Of Box, String)(boxDim)
Console.WriteLine("Boxes equality by dimensions:")
Dim redBox As New Box(8, 4, 8)
Dim greenBox As New Box(8, 6, 8)
Dim blueBox As New Box(8, 4, 8)
Dim yellowBox As 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:")
Dim boxVolume As New BoxSameVolume()
boxes = New Dictionary(Of Box, String)(boxVolume)
Dim pinkBox As New Box(8, 4, 8)
Dim orangeBox As New Box(8, 6, 8)
Dim purpleBox As New Box(4, 8, 8)
Dim brownBox As New Box(8, 8, 4)
AddBox(pinkBox, "pink")
AddBox(orangeBox, "orange")
AddBox(purpleBox, "purple")
AddBox(brownBox, "brown")
End Sub
Public Sub AddBox(ByVal bx As Box, ByVal name As String)
Try
boxes.Add(bx, name)
Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}", _
name, boxes.Count.ToString(), bx.GetHashCode())
Catch generatedExceptionName As ArgumentException
Console.WriteLine("A box equal to {0} is already in the collection.", name)
End Try
End Sub
End Module
Public Class Box
Public Sub New(ByVal h As Integer, ByVal l As Integer, ByVal w As Integer)
Me.Height = h
Me.Length = l
Me.Width = w
End Sub
Private _Height As Integer
Public Property Height() As Integer
Get
Return _Height
End Get
Set(ByVal value As Integer)
_Height = value
End Set
End Property
Private _Length As Integer
Public Property Length() As Integer
Get
Return _Length
End Get
Set(ByVal value As Integer)
_Length = value
End Set
End Property
Private _Width As Integer
Public Property Width() As Integer
Get
Return _Width
End Get
Set(ByVal value As Integer)
_Width = value
End Set
End Property
End Class
Class BoxSameDimensions : Inherits EqualityComparer(Of Box)
Public Overloads Overrides Function Equals(ByVal b1 As Box, _
ByVal b2 As Box) As Boolean
If b1 Is Nothing AndAlso b2 Is Nothing Then
Return True
Else If b1 Is Nothing OrElse b2 Is Nothing Then
Return False
End If
Return (b1.Height = b2.Height AndAlso b1.Length = b2.Length _
AndAlso b1.Width = b2.Width)
End Function
Public Overloads Overrides Function GetHashCode(ByVal bx As Box) As Integer
Dim hCode As Integer = bx.Height Xor bx.Length Xor bx.Width
Return hCode.GetHashCode()
End Function
End Class
Class BoxSameVolume : Inherits EqualityComparer(Of Box)
Public Overloads Overrides Function Equals(ByVal b1 As Box, _
ByVal b2 As Box) As Boolean
If b1 Is Nothing AndAlso b2 Is Nothing Then
Return True
Else If b1 Is Nothing OrElse b2 Is Nothing Then
Return False
End If
Return (b1.Height * b1.Width * b1.Length = _
b2.Height * b2.Width * b2.Length)
End Function
Public Overloads Overrides Function GetHashCode(ByVal bx As Box) As Integer
Dim hCode As Integer = bx.Height * bx.Length * bx.Width
Return hCode.GetHashCode()
End Function
End Class
' 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 |
返回一个默认的相等比较器,用于比较此泛型自变量指定的类型。 |
方法
Create(Func<T,T,Boolean>, Func<T,Int32>) |
EqualityComparer<T>通过使用指定的委托作为比较器 和 GetHashCode(T) 方法的Equals(T, T)实现来创建 。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
Equals(T, T) |
当在派生类中被重写时,确定两个类型为 |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetHashCode(T) |
在派生类中重写时,用作指定对象的哈希算法和数据结构(如哈希表)的哈希函数。 |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
显式接口实现
IEqualityComparer.Equals(Object, Object) |
确定指定的对象是否相等。 |
IEqualityComparer.GetHashCode(Object) |
返回指定对象的哈希代码。 |