Comparer<T>.Default Property

Comparer<T>.Default doesn't use your FooComparer class. It simply returns an instance of the internal class GenericComparer<T>.
This class has the constraint that T must implement IComparable<T> so it can simply delegate the calls to it Compare method to the Compare methods of the instances it gets passed.

Something like this:

internal class GenericComparer<T> : Comparer<T> where T : IComparable<T>
{
public override int Compare(T x, T y)
{
if (x != null)
{
if (y != null)
return x.CompareTo(y);
return ;
}
else
{
if (y != null)
return -;
return ;
}
} // ...
}

IComparable<T> Vs. IComparer<T>

As the name suggests, IComparable<T> reads out I'm comparableIComparable<T> when defined for T lets you compare the current instance with another instance of same type. IComparer<T>reads out I'm a comparer, I compareIComparer<T> is used to compare any two instances of T, typically outside the scope of the instances of T.

As to what they are for can be confusing at first. From the definition it should be clear that hence IComparable<T> (defined in the class T itself) should be the de facto standard to provide the logic for sorting. The default Sort on List<T> etc relies on this. Implementing IComparer<T> on Tdoesn't help regular sorting. Subsequently, there is little value for implementing IComparable<T> on any other class other than T. This:

class MyClass : IComparable<T>

rarely makes sense. On the other hand

class T : IComparable<T>
{
public int CompareTo(T other)
{
//....
}
}

is how it should be done.

IComparer<T> can be useful when you require sorting based on a custom order, but not as a general rule. For instance, in a class of Person at some point you might require to Sort people based on their age. In that case you can do:

class Person
{
public int Age;
} class AgeComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Age - y.Age;
}
}

Now the AgeComparer helps in sorting a list based on Age.

var people = new Person[] { new Person { age =  }, new Person(){ age =  } };
people.Sort(p, new AgeComparer()); //person with age 22 comes first now.

Similarly IComparer<T> on T doesn't make sense.

class Person : IComparer<Person>

True this works, but doesn't look good to eyes and defeats logic.

Usually what you need is IComparable<T>. Also ideally you can have only one IComparable<T> while multiple IComparer<T> is possible based on different criteria.

The IComparer<T> and IComparable<T> are exactly analogous to IEqualityComparer<T> and IEquatable<T> which are used for testing equality rather than comparing/sorting; a good threadhere where I wrote the exact same answer :)

When to use IComparable<T> or IComparer<T>

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 thisinstance. IComparer<T> is useful for sorting collections as the IComparer<T> stands outside of the comparison.

Quote From:

Comparer<T>.Default Property

difference between IComparable and IComparer

When to use IComparable<T> Vs. IComparer<T>

C#/.NET Little Wonders: Comparer<T>.Default

Comparer<T> IComparer<T> IComparable<T>的更多相关文章

  1. .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解

    本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...

  2. CSharp - Comparison between IComparer and IComparable

    /* Author: Jiangong SUN */ I've already written an article introducing the usage of comparer here. I ...

  3. Icomparer和Icomparable集合排序

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

  4. IComparer 与 IComparable

    static void Main() { var people = new ArrayList(); people.AddRange(new ArrayList { }, }, }, } }); Co ...

  5. IComparer、IComparable、StringComparison枚举、CultureInfo 的用法

    IEnumerable<T> 和 IEnumerator<T>.泛型版本是新式代码的首要选项. InvariantCulture:程序间.程序数据库.程序网络交互用Invari ...

  6. 转载:C#中的泛型

    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...

  7. C#中的泛型 【转】

    C#中的泛型 泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确 ...

  8. C# Generic(转载)

    型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具体 ...

  9. C#中的泛型详解

    泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...

随机推荐

  1. Caffe Ubuntu14.04 64位 的最快安装 (cuda7.5 + cudnn7.0 2016最新)

    最近因为各种原因,装过不少次Caffe,安装过程很多坑,为节省新手的时间,特此总结整个安装流程. 关于Ubuntu 版本的选择,建议用14.04这个比较稳定的版本,但是千万不要用麒麟版!!!比原版体验 ...

  2. 一步一步深入spring(2)-三种方式来实例化bean

    在一步一步深入spring(1)--搭建和测试spring的开发环境中提到了一种实例化bean的方式,也是最基本的使用构造器实例化bean 1.使用构造器实例化bean:这是最简单的方式,Spring ...

  3. NDepend 3.0已与Visual Studio集成

    NDepend 3.0已与Visual Studio集成 投递人 itwriter 发布于 2010-02-10 16:17 评论(0) 有1638人阅读  原文链接  [收藏]  « » NDepe ...

  4. 【2013Esri全球用户大会精彩看点】Jack为您全面解读“GIS-Transforming Our World”

    GIS正影响着最尖端的科学与技术,正改变着我们的世界. 1.     GIS的带来的改变不只是物质世界的,还有观念方面. 当今世界面临各种挑战,我们要创造更美好的未来,需要智能的GIS.GIS改变了我 ...

  5. anadonca环境配置和模块安装

    1.最方便的python环境配置: 下载anaconda即可,自带spyder,集成科学计算的库,自带pip,不用折腾. 想用sublime编写python并运行的话,需要自己配置编译环境,并下载插件 ...

  6. 使用 IDEA 创建 Maven Web 项目 (四)- 让 WEB 应用跑起来

    在 IDEA 中配置 Tomcat 单击 IDEA 工具栏上的 Edit Configurations... (在一个下拉框中),弹出 Run/Debug Configurations 对话框. 单击 ...

  7. Java基础篇Socket网络编程中的应用实例

    说到java网络通讯章节的内容,刚入门的学员可能会感到比较头疼,应为Socket通信中一定会伴随有IO流的操作,当然对IO流比较熟练的哥们会觉得这是比较好玩的一章,因为一切都在他们的掌握之中,这样操作 ...

  8. windows下练习linux shell

    <---开始学习linux---记录一下---路漫漫其修远兮---加油吧---萌萌达> 使用软件:Cygwin  下载地址(免安装版):链接: http://pan.baidu.com/s ...

  9. Oracle多关键字查询

    因项目需要,在某查询页面的查询字段支持多关键字查询,支持空格隔开查询条件,故实现如下: 使用的原理是:ORACLE中的支持正则表达式的函数REGEXP_LIKE, '|' 指明两项之间的一个选择.例子 ...

  10. [ios] NSURL

    NSLog(@“Scheme: %@”, [url scheme]); NSLog(@“Host: %@”, [url host]); NSLog(@“Port: %@”, [url port]); ...