请演示一下IComparable 接口和 IComparer接口用法。不太懂。

解决方案 »

  1.   

    msdn上有啊 学c#的最大好处是有强大的msdn还是中文的
    http://msdn.microsoft.com/zh-cn/library/system.icomparable.aspxusing System;
    using System.Collections;public class Temperature : IComparable 
    {
        // The temperature value
        protected double temperatureF;    public int CompareTo(object obj) {
            if(obj is Temperature) 
            {
                Temperature otherTemperature = (Temperature) obj;
                return this.temperatureF.CompareTo(otherTemperature.temperatureF);//
            }
            else
            {
               throw new ArgumentException("Object is not a Temperature");
            }    
        }    public double Fahrenheit 
        {
            get 
            {
                return this.temperatureF;
            }
            set {
                this.temperatureF = value;
            }
        }    public double Celsius 
        {
            get 
            {
                return (this.temperatureF - 32) * (5/9);
            }
            set 
            {
                this.temperatureF = (value * 9/5) + 32;
            }
        }
    }public class CompareTemperatures
    {
       public static void Main()
       {
          ArrayList temperatures = new ArrayList();
          // Initialize random number generator.
          Random rnd = new Random();      // Generate 10 temperatures between 0 and 100 randomly.
          for (int ctr = 1; ctr <= 10; ctr++)
          {
             int degrees = rnd.Next(0, 100);
             Temperature temp = new Temperature();
             temp.Fahrenheit = degrees;
             temperatures.Add(temp);   
          }      // Sort ArrayList.
          temperatures.Sort();      foreach (Temperature temp in temperatures)
             Console.WriteLine(temp.Fahrenheit);   }
    }
    // The example displays the following output to the console (individual
    // values may vary because they are randomly generated):
    //       2
    //       7
    //       16
    //       17
    //       31
    //       37
    //       58
    //       66
    //       72
    //       95
      

  2.   

    return this.temperatureF.CompareTo(otherTemperature.temperatureF);// 可以改为this.temperatureF-otherTemperature.temperatureF例子加入了 double.compareto方法
      

  3.   

    public class Temperature : IComparable 

        // The temperature value 
        protected double temperatureF;     public int CompareTo(object obj) { 
            if(obj is Temperature) 
            { 
                Temperature otherTemperature = (Temperature) obj; 
                return this.temperatureF.CompareTo(otherTemperature.temperatureF);// 
            } 
            else 
            { 
              throw new ArgumentException("Object is not a Temperature"); 
            }    
        } 
    this.temperatureF这里是什么回事?这不是一个成员变量吗?怎么还可以调用.CompareTo函数?
      

  4.   

    实现这个接口的CompareTo,怎么实现法,这么怪的。
      

  5.   

    如果用double,编译器会把它编译成Double,所以如果直接用Double就可以让编译器少做一点点工作http://msdn.microsoft.com/zh-cn/library/system.double.compareto.aspx
      

  6.   

    public class Temperature : IComparable 

        // The temperature value 
        protected double temperatureF;     public int CompareTo(object obj) { 
            if(obj is Temperature) 
            { 
                Temperature otherTemperature = (Temperature) obj; 
                return this.temperatureF.CompareTo(otherTemperature.temperatureF);// 
            } 
            else 
            { 
              throw new ArgumentException("Object is not a Temperature"); 
            }    
        } 
    问题是这样的,temperatureF不是一个双精度的成员,CompareTo又是一个成员,怎么在CompareTo成员方法里,用this.temperatureF.CompareTo()怎么不是this.CompareTo()..,奇怪,不明白这里。