当前位置 > 首页 > Asp.net

Using IComparable<T> Interface

2010-4-12 8:08:00来源:Asp.net

Level : Beginner to Intermediate

C# language has constantly evolved over a constant period of time.Each new version introduced new features which changed the way we programmed and solved the problems. Whether it was introduction of generics in C# 2.0 , LINQ in C# 3.0 or concept of dynamic programming in C# 4.0 , each of them had or will have greater impact on our programming style.As a developer we don’t have much option but to evolve and redefine our self in this constantly changing environment. That said , I personally think that its even more important now to understand the fundamentals and central concept of C#. Its only through the proper understanding of these fundamental concepts , that we can get full grasp on advanced concepts like lambda expressions or LINQ. So with the overall intent being made clear , lets start with IComparable<T> interface.

IComparable<T>

Lets start with a very basic example wherein I will be creating a List of integer and then I will call the Sort() method on that list.

List<int> lstInt = new List<int>() { 23, 2, 1, 34, 12, 17 };
lstInt.Sort();
 
foreach (Int32 number in lstInt)
{
     Console.WriteLine(number.ToString());
}

OutPut :- 1,2,12,17,23,34

In here , Sort() method sort’s the list of integers. If we can recall from what we studied in data structures , sorting is implemented by algorithms like Bubble sort , merge sort etc.Internally .Net framework uses which sorting technique is not a matter of concern for us.All I know and care about is that , for sorting I should be able to compare two values. i.e. I should be able to compare 23 with 2. And in .Net framework types make themselves comparable by implementing IComparable<T> interface.So in the above case ,  integer type internally implements IComparable<T> interface.In short while we will see how Integer type implements IComparable<T> interface,but before that lets see the signature of IComparable<T> interface.

IComparable<T> Interface

   1:  public interface IComparable<T>
   2:  {
   3:      // Methods
   4:      int CompareTo(T other);
   5:  } 

Fairly simple , just one method which returns an integer.The possible values that CompareTo can return are –1 , 0 , 1. –1 is when initial value is less than the later one , 0 when both are equal and 1 when initial value is greater than later one.

   1:  if(a < b) 
   2:      return -1
   3:  else if(a > b)
   4:      return 1
   5:  else
   6:      return 0;

Next very quickly lets have a look at how System.Int32 type implements IComparable<T> interface.

   1:  public int CompareTo(int value)
   2:  {

  • Windows Mobile开发文章收藏

    2009-6-13 23:27:00

    入门或理论研究系列文章 智能手机 手机词汇 研发手机基本流程 我理解的Windows moblie J2ME,CompactFram

  • 用Ajax 1.0制作一个按钮提交后UpdatePanel动画

    2007-7-10 12:11:00

    摘要: 这里我制使用了Ajax 1.0制作了当一个LinkButton点击提交后UpdatePanel闪出一个边框的动画. 阅读全文 winnerzone 2007-07-10 11:0

  • 使用silverlight构建一个工作流设计器(七)(附最新源代码及在线演示)

    2009-4-29 19:41:00

    最新源代码下载: http://workflowdesigner.codeplex.com/ 最新版本在线演示: http://219.153.66.61/wf 本文系

  • So I Created Another Utils Library

    2010-7-14 8:17:00

    Thought it was time I start my own Utils library (why not everyone else seems to have one), I have

  • 也谈博客园的赢利模式 ---回应Where is the money??? – Here

    2007-2-13 5:23:00

    回应Where is the money??? – Here 博客园赚穷酸博客的钱是没有出路的. 博客园不缺技术人才资源,优势是技术人员的信息平台,程序人不是啡厅的吧的小K,

  • ModelBinder——ASP.NET MVC Model榜定的核心

    2012-5-21 8:31:00

    Model的绑定体现在从当前请求提取相应的数据绑定到目标Action方法的参数。通过前面的介绍我们知道Action方法的参数通过ParameterDescriptor来描述,ParameterDes

  • mvc-mini-profiler - An effective mini-profiler for ASP.NET MVC

    2011-6-13 17:07:00

    The mvc-mini-profiler is a lightweight, but effective mini profiler tool for ASP.NET apps specially

  • 求助一个p2p的测试算法

    2009-8-16 15:41:00

    摘要: 我做了一个P2P的系统,想测试一下它的性能,于是想做一个测试程序,我把数据都收集好了,可是对数据的分析的算法却无从下手,各位大侠们感兴趣的不妨试一下如何解决这个问题。 问题前奏:被测试的程

  • QCon日记

    2009-4-8 1:08:00

    6日晚,参会的InfoQ编辑和国际讲师们一起在恭王府边的四川饭店腐败了一次。腐败的地方选得很好,居然就在清朝第一贪官和绅府的旁边。这是我第 一次参加InfoQ的线下活动,也是与中文站编辑的初次谋面。

  • 自定义.NET Calarder日历控件1

    2012-1-29 16:48:00

    这几天看了下.net 日历控件,去Google了下出来很多,大家都知道.netframework版本中自带的日历控件只有日期没有备注的,比如我在1月1日下面要显示出‘元旦节’