IEqualityComparer<T> 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义用于支持比较对象是否相等的方法。
generic <typename T>
public interface class IEqualityComparer
public interface IEqualityComparer<in T>
public interface IEqualityComparer<T>
type IEqualityComparer<'T> = interface
Public Interface IEqualityComparer(Of In T)
Public Interface IEqualityComparer(Of T)
类型参数
- 派生
示例
以下示例将自定义 Box
对象添加到字典集合。 如果对象维度相同,则 Box
被视为相等。
using System;
using System.Collections.Generic;
class Example
{
static void Main()
{
BoxEqualityComparer boxEqC = new BoxEqualityComparer();
var boxes = new Dictionary<Box, string>(boxEqC);
var redBox = new Box(4, 3, 4);
AddBox(boxes, redBox, "red");
var blueBox = new Box(4, 3, 4);
AddBox(boxes, blueBox, "blue");
var greenBox = new Box(3, 4, 3);
AddBox(boxes, greenBox, "green");
Console.WriteLine();
Console.WriteLine("The dictionary contains {0} Box objects.",
boxes.Count);
}
private static void AddBox(Dictionary<Box, String> dict, Box box, String name)
{
try {
dict.Add(box, name);
}
catch (ArgumentException e) {
Console.WriteLine("Unable to add {0}: {1}", box, e.Message);
}
}
}
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; }
public override String ToString()
{
return String.Format("({0}, {1}, {2})", Height, Length, Width);
}
}
class BoxEqualityComparer : IEqualityComparer<Box>
{
public bool Equals(Box b1, Box b2)
{
if (b2 == null && b1 == null)
return true;
else if (b1 == null || b2 == null)
return false;
else if(b1.Height == b2.Height && b1.Length == b2.Length
&& b1.Width == b2.Width)
return true;
else
return false;
}
public int GetHashCode(Box bx)
{
int hCode = bx.Height ^ bx.Length ^ bx.Width;
return hCode.GetHashCode();
}
}
// The example displays the following output:
// Unable to add (4, 3, 4): An item with the same key has already been added.
//
// The dictionary contains 2 Box objects.
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim boxEqC As New BoxEqualityComparer()
Dim boxes As New Dictionary(Of Box, String)(boxEqC)
Dim redBox = New Box(4, 3, 4)
AddBox(boxes, redBox, "red")
Dim blueBox = new Box(4, 3, 4)
AddBox(boxes, blueBox, "blue")
Dim greenBox = new Box(3, 4, 3)
AddBox(boxes, greenBox, "green")
Console.WriteLine()
Console.WriteLine("The dictionary contains {0} Box objects.",
boxes.Count)
End Sub
Private Sub AddBox(dict As Dictionary(Of Box, String), box As Box, name As String)
Try
dict.Add(box, name)
Catch e As ArgumentException
Console.WriteLine("Unable to add {0}: {1}", box, e.Message)
End Try
End Sub
End Module
Public Class Box
Private _Height As Integer
Private _Length As Integer
Private _Width As Integer
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
Public Property Height() As Integer
Get
Return _Height
End Get
Set(ByVal value As Integer)
_Height = value
End Set
End Property
Public Property Length() As Integer
Get
Return _Length
End Get
Set(ByVal value As Integer)
_Length = value
End Set
End Property
Public Property Width() As Integer
Get
Return _Width
End Get
Set(ByVal value As Integer)
_Width = value
End Set
End Property
Public Overrides Function ToString() As String
Return String.Format("({0}, {1}, {2})", _Height, _Length, _Width)
End Function
End Class
Class BoxEqualityComparer
Implements IEqualityComparer(Of Box)
Public Overloads Function Equals(ByVal b1 As Box, ByVal b2 As Box) _
As Boolean Implements IEqualityComparer(Of Box).Equals
If b1 Is Nothing AndAlso b2 Is Nothing Then
Return True
ElseIf b1 Is Nothing Or b2 Is Nothing Then
Return False
ElseIf b1.Height = b2.Height AndAlso b1.Length =
b2.Length AndAlso b1.Width = b2.Width Then
Return True
Else
Return False
End If
End Function
Public Overloads Function GetHashCode(ByVal bx As Box) _
As Integer Implements IEqualityComparer(Of Box).GetHashCode
Dim hCode As Integer = bx.Height Xor bx.Length Xor bx.Width
Return hCode.GetHashCode()
End Function
End Class
' The example displays the following output:
' Unable to add (4, 3, 4): An item with the same key has already been added.
'
' The dictionary contains 2 Box objects.
注解
此接口允许实现集合的自定义相等比较。 也就是说,你可以为类型 T
创建自己的相等性定义,并指定此定义与接受泛型接口的 IEqualityComparer<T> 集合类型一起使用。 在.NET Framework中,泛型集合类型的构造函数Dictionary<TKey,TValue>接受此接口。
此接口的默认实现由 Default 泛型类的属性 EqualityComparer<T> 提供。 类 StringComparer 实现 IEqualityComparer<T> 类型 String。
此接口仅支持相等比较。 对排序和排序的比较的自定义由 IComparer<T> 泛型接口提供。
建议派生自 EqualityComparer<T> 类而不是实现 IEqualityComparer<T> 接口,因为 EqualityComparer<T> 类使用 IEquatable<T>.Equals 方法而不是 Object.Equals 方法测试相等性。 这与Contains``Remove``IndexOf``LastIndexOf
类和其他泛型集合的方法Dictionary<TKey,TValue>一致。
方法
Equals(T, T) |
确定指定的对象是否相等。 |
GetHashCode(T) |
返回指定对象的哈希代码。 |