IEqualityComparer<T> VS System.IEquatable<T>

http://www.cnblogs.com/rush/archive/2012/09/30/2709113.html

类似于IComparer<T> VS IComparable<T>.

public interface IComparable<in T>

{    // Methods    

    int CompareTo(T other);

}

public interface IComparer<in T>

{    // Methods    

    int Compare(T x, T y);

}

public interface IEquatable<T>

{    // Methods    

    bool Equals(T other);

}

public interface IEqualityComparer<in T>

{    // Methods   

    bool Equals(T x, T y);    

    int GetHashCode(T obj);

}

IEqualityComparer<T>是用于custom 比较是否相等的,而System.IEquatable<T>是用于default 比较的。即某个class 会实现 IEquatable<T>来定义相等操作,而 IEqualityComparer<T> 则用于自定义判断。例如用在LinkedList.Find(T value)方法中。

EqualityComparer<T> comparer = EqualityComparer<T>.Default;

以string 类为例来说明:

public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>

例如Dictionary<TKey, TValue>的key 不可以重复,当调用Add方法时就会判断是否相等。

对比Container类的构造函数:

public Dictionary(int capacity, IEqualityComparer<TKey> comparer); //Dictionary需要对key进行判断是否相等的操作所以需要实现IEqualityComparer接口的对象

public LinkList();  //不需要sort,也不需要判断添加的对象是否相等所以不需要comparer

public List(); 动态数组. 仅在sort方法中需要提供一个comparer对象,如果没有则使用集合中存储对象默认的比较方法即要求被存储类必须实现IComparable接口

public BinarySearchTree(IComparer<T> comparer);  二叉树需要对添加的元素进行比较。 this.comparer = Comparer<T>.Default;

public SortSet(IComparer<T> comparer);  红黑树也需要对添加的元素进行比较

[csharp]  view plain copy
  1. using System;  
  2.     using System.Collections.Generic;  
  3.   
  4.     class Program  
  5.     {  
  6.         static Dictionary<Box, String> boxes;  
  7.   
  8.         static void Main(string[] args)  
  9.         {  
  10.   
  11.             BoxSameDimensions boxDim = new BoxSameDimensions();  
  12.             boxes = new Dictionary<Box, string>(boxDim);  
  13.   
  14.             Console.WriteLine("Boxes equality by dimensions:");  
  15.             Box redBox = new Box(8, 4, 8);  
  16.             Box greenBox = new Box(8, 6, 8);  
  17.             Box blueBox = new Box(8, 4, 8);  
  18.             Box yellowBox = new Box(8, 8, 8);  
  19.             AddBox(redBox, "red");  
  20.             AddBox(greenBox, "green");  
  21.             AddBox(blueBox, "blue");  
  22.             AddBox(yellowBox, "yellow");  
  23.   
  24.             Console.WriteLine();  
  25.             Console.WriteLine("Boxes equality by volume:");  
  26.   
  27.             BoxSameVolume boxVolume = new BoxSameVolume();  
  28.             boxes = new Dictionary<Box, string>(boxVolume);  
  29.             Box pinkBox = new Box(8, 4, 8);  
  30.             Box orangeBox = new Box(8, 6, 8);  
  31.             Box purpleBox = new Box(4, 8, 8);  
  32.             Box brownBox = new Box(8, 8, 4);  
  33.             AddBox(pinkBox, "pink");  
  34.             AddBox(orangeBox, "orange");  
  35.             AddBox(purpleBox, "purple");  
  36.             AddBox(brownBox, "brown");  
  37.         }  
  38.   
  39.         public static void AddBox(Box bx, string name)  
  40.         {  
  41.             try  
  42.             {  
  43.                 boxes.Add(bx, name);  
  44.                 Console.WriteLine("Added {0}, Count = {1}, HashCode = {2}",  
  45.                     name, boxes.Count.ToString(), bx.GetHashCode());  
  46.             }  
  47.             catch (ArgumentException)  
  48.             {  
  49.                 Console.WriteLine("A box equal to {0} is already in the collection.", name);  
  50.             }  
  51.   
  52.    
  53.   
  54.   
  55.         }  
  56.     }  
  57.     public class Box  
  58.     {  
  59.         public Box(int h, int l, int w)  
  60.         {  
  61.             this.Height = h;  
  62.             this.Length = l;  
  63.             this.Width = w;  
  64.         }  
  65.         public int Height { getset; }  
  66.         public int Length { getset; }  
  67.         public int Width { getset; }  
  68.     }  
  69.   
  70.   
  71.     class BoxSameDimensions : EqualityComparer<Box>  
  72.     {  
  73.   
  74.         public override bool Equals(Box b1, Box b2)  
  75.         {  
  76.             if (b1.Height == b2.Height & b1.Length == b2.Length  
  77.                                 & b1.Width == b2.Width)  
  78.             {  
  79.                 return true;  
  80.             }  
  81.             else  
  82.             {  
  83.                 return false;  
  84.             }  
  85.         }  
  86.   
  87.   
  88.         public override int GetHashCode(Box bx)  
  89.         {  
  90.             int hCode = bx.Height ^ bx.Length ^ bx.Width;  
  91.             return hCode.GetHashCode();  
  92.         }  
  93.   
  94.     }  
  95.   
  96.     class BoxSameVolume : EqualityComparer<Box>  
  97.     {  
  98.   
  99.         public override bool Equals(Box b1, Box b2)  
  100.         {  
  101.             if (b1.Height * b1.Width * b1.Length ==  
  102.                 b2.Height * b2.Width * b2.Length)  
  103.             {  
  104.                 return true;  
  105.             }  
  106.             else  
  107.             {  
  108.                 return false;  
  109.             }  
  110.         }  
  111.   
  112.   
  113.         public override int GetHashCode(Box bx)  
  114.         {  
  115.             int hCode = bx.Height ^ bx.Length ^ bx.Width;  
  116.             return hCode.GetHashCode();  
  117.         }  
  118.   
  119.     }  
  120.     /* This example produces the following output: 
  121.      *  
  122.         Boxes equality by dimensions: 
  123.         Added red, Count = 1, HashCode = 46104728 
  124.         Added green, Count = 2, HashCode = 12289376 
  125.         A box equal to blue is already in the collection. 
  126.         Added yellow, Count = 3, HashCode = 55530882 
  127.  
  128.         Boxes equality by volume: 
  129.         Added pink, Count = 1, HashCode = 30015890 
  130.         Added orange, Count = 2, HashCode = 1707556 
  131.         A box equal to purple is already in the collection. 
  132.         A box equal to brown is already in the collection. 
  133.      *  
  134.     */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值