当前位置 > 首页 > 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:  {

  • Cool Bing Maps Silverlight Apps

    2010-1-28 13:29:00

    If your using the Silverlight version of Bing maps , additional apps are available when you click

  • 《死了都要TRY》——迎接2010

    2010-1-1 2:20:00

    阅读: 109 评论: 1 作者: birdshome 发表于 2010-01-01 00:28 原文链接 其实是很久很久前写的了,本来想码上几个人,唱出来再搞个山寨MTV啥的,结果实在

  • 看” 关于技术人员创业的讨论”有感

    2007-6-1 9:19:00

    摘要: dudu的“关于技术人员创业的讨论”的帖子已经有些时日了,早就有一些想法,直到今天才写下此文。也不知大家的热潮过去了没有。不是故作文章,只是这些年来,自己也处在这样的尴尬局面,很是不

  • 我完成的C#关于在lucene下的中文切词

    2007-9-26 18:30:00

    摘要: 经过一天的研究,终于完成了在lucene.net下可以使用的中文切词方法。感到有些复杂,不过我还是拿下了。颇有点成就感的,发上来跟大家分享一下! 在实现了中文切词的基础方法上,我将其封装在继

  • GetValue<T>() extension method for RegistryKey class

    2009-8-20 20:52:00

    I needed some elegant method to read registry values in my Live Writer Delicious bookmarks plug-in

  • HubbleDotNet 分布式检索算法介绍 (一)

    2011-5-18 9:23:00

    作者:eaglet 转载请注明出处 全文索引的分布式检索粗想想似乎很简单,感觉就是把多个接入点搜索出来的数据做个合并排序就可以,但如果想要做好,满足商业应用要求,这里面涉及到很多算法优化的问

  • FluentPath: a fluent wrapper around System.IO

    2010-3-11 13:45:00

    .NET is now more than eight years old, and some of its APIs got old with more grace than others. S

  • DotNet Remoting中自带的Tcp和Http信道是无法使用事件的

    2007-4-15 10:06:00

    摘要: 园子里的Bruce Zhang(以前的wayfarer)写的Remoting文章很全面,开始学习时他的文章帮了很大的忙(在这里先谢了),他写的Remoting事件尤为精采,后来在一个项目中用

  • 实战 HTTP 处理程序(HTTP Handler) (1) -- 创建一个最简单的 HTTP Handler

    2007-9-1 15:04:00

    摘要: 看过《实战 HTTP 处理程序(HTTP Handler)》系列的全部文章后,您就可以 - 轻松创建您自己的自定义 HTTP 处理程序 - 让您的HTTP 处理程序与We

  • jQuery Ajax 方法调用 Asp.Net WebService 的详细例子(原创)

    2009-2-1 23:01:00

    摘要: 这很常用,搜索了一下博客园的“找找看”和谷歌,看到大部分都是转载于一两篇文章(而且来源还不是博客园),有的是简单的说一点无法运行,给初学者的调试和学习带来不方便,我