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)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...
随机推荐
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- Java静态代理和动态代理
今天介绍一下代理设计模式,在业务场景中使用代理模式的好处有很多,包括什么权限校验,事务管理等等,具体有什么好处大家自动百度吧,我这里只解释代理模式的设计原理.首先这个设计模式出来的时候先是静态代理模式 ...
- hibernate4 spring3.2 事务不提交分析
最近在做微信项目,我搭建了一个基于servlet,spring3.2,hibernate4.1的框架.因为基于消息的servlet和基于业务层是分开做的,也就是先把业务层做了,再去将所有的请求转到业务 ...
- Android中Bitmap, Drawable, Byte之间的转化
1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap. ...
- 怎么在Windows下安装Linux虚拟机
前提:①电脑有安装好VMware Workstation Pro虚拟机,没有的话点此下载并安装.②下载好CentOS-7-x86_64-DVD-1503-01镜像文件,没有点此下载密码:lomg. 1 ...
- Js-Html 前端系列--页面撑开头尾
今天学习过程中,发现一个超实用的方法,就是当页面有尾部,但是内容又不多的情况下,让中间的内容把尾部撑到底部. <script type="text/javascript"&g ...
- 关于Ueditor 前后端分离实现文件上传到独立服务器的问题 望大神们赐教
最近,由于网站实现多台服务器负载均衡,导致编辑器上传文件需要同步,可是使用同步软件太慢,不太现实,所以想到实现编辑器上传文件直接上传到独立文件服务器.可是没想到遇到坑了. 1.在本地IIS 中添加网站 ...
- 前端MVC学习笔记(四)——NodeJS+MongoDB+AngularJS+Bootstrap书店示例
这章的目的是为了把前面所学习的内容整合一下,这个示例完成一个简单图书管理模块,因为中间需要使用到Bootstrap这里先介绍Bootstrap. 示例名称:天狗书店 功能:完成前后端分离的图书管理功能 ...
- php集成环境和自己配置的区别,php集成环境、php绿色集成环境、php独立安装版环境这三者的区别
最近有学生问我,直接使用PHP集成环境和我们自己独立安装的php环境有什么不一样吗? 答:PHP集成环境,和自己安装的php环境实际上没啥区别的,只不过大部分的集成环境进行了一些绿化操作,本质上没啥区 ...
- python调用ansible api 2.0 运行playbook带callback返回
# -*- coding:utf8 -*- ''' Created on 2017年1月13日 @author: qiancheng ''' import os import json from co ...