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. declaration is incompatible with "__nounwind __interwork __softfp unsigned long __get_PSP(void)" IAR 编译报故障

    原因是以前的CMSIS  CORTEX-CM0 文件太老了.   使用新文件就可以.

  2. 《深入浅出Node.js》第5章 内存控制(未完)

    @by Ruth92(转载请注明出处) 第5章 内存控制 基于无阻塞.事件驱动建立的 Node 服务,具有内存消耗低的优点,非常适合处理海量的网络请求. 内存控制正是在海量请求和长时间运行的前提下进行 ...

  3. vs 下安装boost

    首先到boost官网去下载最新的版本的boost库,选对对应的平台版本. 下载官网 解压文件, 先运行bootstrap.bat文件,生成bjam.exe并运行它.推荐完全编译的方式运行bjam 推荐 ...

  4. 遥感影像滤波处理软件 — timesat3.2

    最近因为要做遥感影像的滤波处理,经过女神推荐,决定用Timesat,可是该软件3.1版本只适合xp系统以及2011的matlab,后来在官网上找到了最新的3.2版本.支持64位操作系统以及2014的m ...

  5. 用MFC如何对子对话框进行初始化

    通常情况下,我们在创建子对话框的类时.cpp文件中并不会自动生成initdialog函数,但我们的很多操作都需要用到initdialog初始化函数,如果你直接在类的头文件中去定义一个初始化函数,然后在 ...

  6. React(JSX语法)-----JSX属性

    1. if you know all the propertities that you want to place on a component ahead of time,it is easy t ...

  7. Java数据类型、变量、运算符、语句。

    数据类型:整型 int long short byte小数 double float 字符 char 转义字符:\'(单引号字符) \\(反斜杠字符) \n(换行) \r(回车) \t(水平制表符,相 ...

  8. Here's to the crazy ones.

    Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square h ...

  9. ubuntu 14.04 下通过apt-get 安装jdk

    Installing default JRE/JDK sudo apt-get update sudo apt-get install default-jre sudo apt-get install ...

  10. c# CLI托管工程开启调试c++库工程代码

    启动调试c#winform工程中,无法命中c++库工程中中的断点,在c#工程中更改调试设置: 勾选上Enable unmanaged code debuging