Comparer<T> IComparer<T> IComparable<T>
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 comparable. IComparable<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 compare. IComparer<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 T
doesn'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 this
instance. 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>的更多相关文章
- .NET Framework System.Array.Sort 数组类,加深对 IComparer、IComparable 以及泛型委托、匿名方法、Lambda 表达式的理解
本文内容 自定义类 Array.Sort 参考资料 System.Array.Sort 有很多对集合的操作,比如排序,查找,克隆等等,你可以利用这个类加深对 IComparer.IComparable ...
- CSharp - Comparison between IComparer and IComparable
/* Author: Jiangong SUN */ I've already written an article introducing the usage of comparer here. I ...
- Icomparer和Icomparable集合排序
c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...
- IComparer 与 IComparable
static void Main() { var people = new ArrayList(); people.AddRange(new ArrayList { }, }, }, } }); Co ...
- IComparer、IComparable、StringComparison枚举、CultureInfo 的用法
IEnumerable<T> 和 IEnumerator<T>.泛型版本是新式代码的首要选项. InvariantCulture:程序间.程序数据库.程序网络交互用Invari ...
- 转载:C#中的泛型
泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...
- C#中的泛型 【转】
C#中的泛型 泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确 ...
- C# Generic(转载)
型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具体 ...
- C#中的泛型详解
泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性.泛型为.NET框架引入了类型参数(type parameters)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...
随机推荐
- myeclipse maven编译出错
从.net 到java 快一年了.这一年学了很多东西.从开发角度来说俩个语言查不到.部署上差异较大.不过java处理问题上确实不太统一.好多问题在网上没有正确的回答.刚换台式机发现 mvn inst ...
- excel==>csv==via phpmyadmin (edit php.ini & my.ini)==> MySQL Database
正如同标题, 标题的顺序是 先从Excel表单,保存为csv文档. 步骤: 1.这个可以用linux下的libra office打开 abc.xls 2.用libra office 将 abc.xls ...
- asp.net MVC 路由机制
1:ASP.NET的路由机制主要有两种用途: -->1:匹配请求的Url,将这些请求映射到控制器 -->2:选择一个匹配的路由,构造出一个Url 2:ASP.NET路由机制与URL重写的区 ...
- ASP.NET发送电子邮件源码示例
using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Mail; try { ...
- Solr之NamedList 简单介绍与实例解析
大家都知道,Solr是一个基于Lucene高可配置的搜索服务器,大部分参数值以及相关优化等等都可以在solrconfig.xml中配置,那么就需要一个能够很快的进行解析和读取配置文件内容的数据结构,为 ...
- VMwarevSphere 服务器虚拟化之二十九 桌面虚拟化之安装View副本服务器
VMwarevSphere 服务器虚拟化之二十九 桌面虚拟化之安装View副本服务器 VMware View中高可用性可是一个必须要考虑的问题.在整个虚拟桌面环境中View Connection S ...
- 根据字节码探讨java自增运算符的原理
public class Test { static int x, y; public static void main(String args[]) { x++; myMethod(); Syste ...
- vim 模式基础操作
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 前言 虽然Emacs已经可以完成大部分的编辑操作,但有时候为了方便也会用到vim.所以记录了 ...
- 快速排序算法C#实现
最近想把几大经典的排序算法系统的整理过一遍,写下笔记,算是复习吧!! 1.快速排序. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部 ...
- 【转】关于MySQL函数GROUP_CONCAT的使用
GROUP_CONCAT()是MySQL数据库提供的一个函数,通常跟GROUP BY一起用,具体可参考MySQL官方文挡:http://dev.mysql.com/doc/refman/5.0/en/ ...