當前位置: 首頁>>代碼示例>>C#>>正文


C# IComparable<T>接口代碼示例

本文整理匯總了C#中System.IComparable<T>接口的典型用法代碼示例。如果您正苦於以下問題:C# IComparable<T>接口的具體用法?C# IComparable<T>怎麽用?C# IComparable<T>使用的例子?那麽, 這裏精選的接口代碼示例或許可以為您提供幫助。


IComparable<T>接口屬於System命名空間,在下文中一共展示了IComparable<T>接口的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CompareTo

//引入命名空間
using System;
using System.Collections.Generic;

public class Temperature : IComparable<Temperature>
{
    // Implement the generic CompareTo method with the Temperature 
    // class as the Type parameter. 
    //
    public int CompareTo(Temperature other)
    {
        // If other is not a valid object reference, this instance is greater.
        if (other == null) return 1;
        
        // The temperature comparison depends on the comparison of 
        // the underlying Double values. 
        return m_value.CompareTo(other.m_value);
    }

    // Define the is greater than operator.
    public static bool operator >  (Temperature operand1, Temperature operand2)
    {
       return operand1.CompareTo(operand2) == 1;
    }
    
    // Define the is less than operator.
    public static bool operator <  (Temperature operand1, Temperature operand2)
    {
       return operand1.CompareTo(operand2) == -1;
    }

    // Define the is greater than or equal to operator.
    public static bool operator >=  (Temperature operand1, Temperature operand2)
    {
       return operand1.CompareTo(operand2) >= 0;
    }
    
    // Define the is less than or equal to operator.
    public static bool operator <=  (Temperature operand1, Temperature operand2)
    {
       return operand1.CompareTo(operand2) <= 0;
    }

    // The underlying temperature value.
    protected double m_value = 0.0;

    public double Celsius    
    {
        get
        {
            return m_value - 273.15;
        }
    }

    public double Kelvin    
    {
        get
        {
            return m_value;
        }
        set
        {
            if (value < 0.0)
            {
                throw new ArgumentException("Temperature cannot be less than absolute zero.");
            }
            else
            {
                m_value = value;
            }
        }
    }

    public Temperature(double kelvins)
    {
        this.Kelvin = kelvins;
    }
}

public class Example
{
    public static void Main()
    {
        SortedList<Temperature, string> temps = 
            new SortedList<Temperature, string>();

        // Add entries to the sorted list, out of order.
        temps.Add(new Temperature(2017.15), "Boiling point of Lead");
        temps.Add(new Temperature(0), "Absolute zero");
        temps.Add(new Temperature(273.15), "Freezing point of water");
        temps.Add(new Temperature(5100.15), "Boiling point of Carbon");
        temps.Add(new Temperature(373.15), "Boiling point of water");
        temps.Add(new Temperature(600.65), "Melting point of Lead");

        foreach( KeyValuePair<Temperature, string> kvp in temps )
        {
            Console.WriteLine("{0} is {1} degrees Celsius.", kvp.Value, kvp.Key.Celsius);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:100,代碼來源:IComparable

輸出:

Absolute zero is -273.15 degrees Celsius.
      Freezing point of water is 0 degrees Celsius.
      Boiling point of water is 100 degrees Celsius.
      Melting point of Lead is 327.5 degrees Celsius.
      Boiling point of Lead is 1744 degrees Celsius.
      Boiling point of Carbon is 4827 degrees Celsius.


注:本文中的System.IComparable<T>接口示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。