Well they are not quite the same thing as IComparer<T> is implemented on a type that is capable of comparing two different objects while IComparable<T> is implemented on types that are able to compare themselves with other instances of the same type.

I tend to use IComparable<T> for times when I need to know how another instance relates to this instance. IComparer<T> is useful for sorting collections as the IComparer<T> stands outside of the comparison.

IComparer < T >是上实现一种能够比较两个不同的对象而IComparable < T >是上实现类型能够比较自己与其他相同类型的实例。

class Student : IComparable
{
public string Name { get; set; }
public int MathScore { get; set; }
public int EnglishScore { get; set; } public int TotalScore
{
get
{
return this.MathScore + this.EnglishScore;
}
} public int CompareTo(object obj)
{
return CompareTo(obj as Student);
} public int CompareTo(Student other)
{
if (other == null)
{
return 1;
}
return this.Name.CompareTo(other.Name);
}
}

But if a teacher 'A' wants to compare students based on MathScore, and teacher 'B' wants to compare students based on EnglishScore. It will be good idea to implement IComparer separately. (More like a strategy pattern)

class CompareByMathScore : IComparer<Student>
{
public int Compare(Student x, Student y)
{
if (x.MathScore > y.MathScore)
return 1;
if (x.MathScore < y.MathScore)
return -1;
else
return 0;
}
}
  1. public class NameComparer:IComparer<Student>
  2. {
  3. public int Compare(Student x, Student y)
  4. {
  5. return x.Name.CompareTo(y.Name);
  6. }
  7. }
 http://stackoverflow.com/questions/3498891/system-comparisont-understanding

System.Comparison<T> is defined as follows:

public delegate int Comparison<in T>(T x, T y);

That means that it's delegate, not a class. A method accepting a delegate as a parameter actually accepts a method, not an instance of a Comparison class.

This code can be rewritten as follows with a lambda expression:

collection.Sort((i1, i2) => i1.ToString().CompareTo(i2.ToString()));

The following snippet might explain better what happens:

public static class TestClass {

  public static void Main(string[] args){
Comparison<Int32> comparisonDelegate = CompareWithCase;
//We now can use comparisonDelegate as though it is a method;
int result = comparisonDelegate(1,2);
} public static int CompareWithCase(int i1, int i2)
{
return i1.ToString().CompareTo(i2.ToString());
}
}
 

IComparable<T> Vs. IComparer<T> System.Comparison<T>的更多相关文章

  1. C# List.Sort与IComparer接口及Comparison委托应用于集合排序

    C#里List.Sort的用法 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  2. 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口

    来源:http://www.cnblogs.com/eagle1986/archive/2011/12/06/2278531.html 1:比较和排序的概念 比较:两个实体类之间按>,=,< ...

  3. C#中的list的System.Predicate<in T>和System.Comparison<in T>的应用

    public class Data { ; ; ; ; public Data() { count++; ma = count; } } //一句话删除满足要求的集合 Asm.RemoveAll((D ...

  4. C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

    在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...

  5. C#中如何使用IComparable<T>与IComparer<T>接口(转载)

    本分步指南描述如何使用两个接口: IComparer和IComparable.在同一篇文章中讨论这些接口有两个原因.经常在一起,使用这些接口和接口类似 (并且有相似的名称),尽管它们用于不同用途. 如 ...

  6. OSMC Vs. OpenELEC Vs. LibreELEC – Kodi Operating System Comparison

    Kodi's two slim-and-trim kid brothers LibreELEC and OpenELEC were once great solutions for getting t ...

  7. Unity3d之-使用BMFont制作美术字体

    一.需求 游戏开发中经常遇到需要以美术字(而非字库)做数字显示的情况,通常美术会提供一组包含单个数字(也会有其它字符)的图片,可能是一张整图,也可能是每个数字分开的散图. 在此我以一张整图这种情况为例 ...

  8. Comparer<T> IComparer<T> IComparable<T>

    Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...

  9. Icomparer和Icomparable集合排序

    c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...

随机推荐

  1. Sunny-Code 最终版总结会议

    设想和目标 我们的软件要解决什么问题?是否定义得很清楚? 我们打算做一款集成小蝴蝶功能.Ip快速修改功能.WiFi共享功能的一款软件.目的是为了解决晚上小蝴蝶断线重连问题.还有新生不会修改IP问题. ...

  2. (原创)VM中的CentOS6.4中安装CloudStack6.3①

    CloudStack是一个功能强大.UI友好的开源云(IaaS)计算解决方案.自Ctrix将CloudStack捐献给 apache 后,一直持续高速发展,其社区活跃度已经渐渐赶上风头一时无两的另一开 ...

  3. Spring Application Event Example

    Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...

  4. c# chart

    1.随便一个例子 string sql = "select distinct count(*) as c,datepart(day,ull_loginTime) as a from user ...

  5. 自适应布局,响应式布局以及rem,em区别

    一.自适应和响应式 先说共同点: 两者都是因为越来越多的 移动设备( mobile, tablet device )加入到互联网中来而出现的为移动设备提供更好的体验的技术.用技术来使网页适应从小到大( ...

  6. React(JSX语法)----JSX拼写

    注意:For DOM differences,such as the inline style attribute,check here. // bad: it displays "FIrs ...

  7. jsp页面动态显示时间

    <SCRIPT language="JavaScript">  function disptime(){ var time = new Date(); var hour ...

  8. poj2368 Buttons Nim取石子游戏

    链接:http://poj.org/problem?id=2368 和前面差距还是很大啊囧 代码: k,a;main(i){,i=;i<=k/&&k%i;++i);k%i||(a ...

  9. css技术

    CSS是英语Cascading Style Sheets(层叠样式表单)的缩写,它是一种用来表现HTML或 XML 等文件式样的计算机语言. CSS 目前最新版本为CSS2,能够真正做到网页表现与内容 ...

  10. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...