IComparable<T> Vs. IComparer<T> System.Comparison<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.
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;
}
}
- public class NameComparer:IComparer<Student>
- {
- public int Compare(Student x, Student y)
- {
- return x.Name.CompareTo(y.Name);
- }
- }
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>的更多相关文章
- C# List.Sort与IComparer接口及Comparison委托应用于集合排序
C#里List.Sort的用法 using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 对象的比较与排序(三):实现IComparable<T>和IComparer<T>泛型接口
来源:http://www.cnblogs.com/eagle1986/archive/2011/12/06/2278531.html 1:比较和排序的概念 比较:两个实体类之间按>,=,< ...
- C#中的list的System.Predicate<in T>和System.Comparison<in T>的应用
public class Data { ; ; ; ; public Data() { count++; ma = count; } } //一句话删除满足要求的集合 Asm.RemoveAll((D ...
- C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法
在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...
- C#中如何使用IComparable<T>与IComparer<T>接口(转载)
本分步指南描述如何使用两个接口: IComparer和IComparable.在同一篇文章中讨论这些接口有两个原因.经常在一起,使用这些接口和接口类似 (并且有相似的名称),尽管它们用于不同用途. 如 ...
- 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 ...
- Unity3d之-使用BMFont制作美术字体
一.需求 游戏开发中经常遇到需要以美术字(而非字库)做数字显示的情况,通常美术会提供一组包含单个数字(也会有其它字符)的图片,可能是一张整图,也可能是每个数字分开的散图. 在此我以一张整图这种情况为例 ...
- Comparer<T> IComparer<T> IComparable<T>
Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...
- Icomparer和Icomparable集合排序
c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...
随机推荐
- [转]架构蓝图--软件架构 "4+1" 视图模型
架构蓝图--软件架构 "4+1" 视图模型 本文基于多个并发视图的使用情况来说明描述软件密集型系统架构的模型.使用多重视图允许独立地处理各"风险承担人":最终用 ...
- mysql 信息查询
show tables information 除了查询数据,管理数据之外,还有其他方面的信息方便和约束着 -- show information about characterset,查询mys ...
- 【JS】数字转大写中文
原文参考 逛到一道面试题,数字转大写中文的,搜索学习并记录于此. //自动转换数字金额为大小写中文字符,返回大小写中文字符串,最大处理到999兆 function changeMoneyToChine ...
- Node.js高效按行输出文件内容
const fs = require('fs'); const EventEmitter = require('events'); const util = require('util'); cons ...
- vector使用篇之erase
由于最近项目使用中发现了之前对vector的一个误区,由此发现自己对vect非常不了解,对此进行了一些了解,由此打算写一下关于vector使用方面的注意点,本篇先来讲一下vector的erase功能, ...
- cocoapods:安装/更新Ruby环境教程
简介 有时候在安装cocoapods时会产生如下错误 ERROR: Error installing cocoapods: activesupport requires Ruby version &g ...
- 【Cocos2d-x 3.x】 调度器Scheduler类源码分析
非个人的全部理解,部分摘自cocos官网教程,感谢cocos官网. 在<CCScheduler.h>头文件中,定义了关于调度器的五个类:Timer,TimerTargetSelector, ...
- C# .Net中七层架构浅析
Model实体层,DBUtility数据访问抽象类,IDAL数据访问接口层,SQLServerDAL数据访问层,DALFactory数据访问工厂类,BLL业务逻辑层,UI界面层 一.项目名称及描述:( ...
- jstl 标签库的使用
JSTL 核心标签库 使用 JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.ot ...
- ORACLE 数据库 MOD 函数用法
1.求2和1的余数. Select mod(2,1) from dual: 2能被1整除所以余数为0. 2.MOD(x,y)返回X除以Y的余数.如果Y是0,则返回X的值. Select mod(2,0 ...