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 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>的更多相关文章
- .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)的概念.类型参数使得设计类和方法时,不必确定一个或多个具 ...
随机推荐
- LigerUI权限系统之用户管理
用户管理较之前的的组织结构和菜单管理稍显复杂.不管怎样还是先上图吧,再来讲解 左边是组织结构,右边是用户,用户是跟组织机构挂钩的,通过点击左边的组织结构,来刷新右边,加载该组织机构下的用户. 用户管理 ...
- 【2013Esri全球用户大会精彩案例】Horry Count GIS--南卡罗来那州霍里县企业级应用
行业领域:政府 基于Online的组织机构订阅服务,构建Horry县的地图及应用门户,满足政府和公众的需要.公众可通过移动设备查看公开的地图,包括教育资源分布.选区分布.墓地管理.害虫监控等内容:政府 ...
- POJ 1013 小水题 暴力模拟
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35774 Accepted: 11 ...
- Java Concurrency (1)
Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable a ...
- Js 获取当前月的天数
function getDays() { //构造当前日期对象 var date = new Date(); //获取年份 var year = date.getFullYear(); //获取当前月 ...
- Arrays类——Arrays.asList()方法使用
java.util类 Arrays java.lang.Object——java.util.Arrays public class Arrays extends Object 此类包含用来操作数组(比 ...
- 从0到1一步步搭建代码质量检测系统~iOS
演示环境:Mac OSX10.12.2 Xcode8 先瞄一眼最终成果- 1.JDK,DBMS(演示环境使用Mysql) 2.创建sonar数据库和用户 mysql -u root -pCREATE ...
- Docker - Docker基础讲义
Docker Docker - 官网 Docker - Hub GitHub - Docker Docker中文社区 虚拟化技术 硬件级虚拟化(hardware-level-virtualizatio ...
- mac nodejs安装
很久没有配置开发环境了,刚换了新电脑,正好借机会重新配置一下node相关的开发环境 安装 nvm :Node Version Manager 由于nodejs版本更新迭代较快,而不同版本间的差异又很大 ...
- Spark集群搭建步骤
问题: 参考:Spark快速入门指南 – Spark安装与基础使用