本文整理汇总了C#中System.IComparable<T>.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# IComparable<T>.CompareTo方法的具体用法?C# IComparable<T>.CompareTo怎么用?C# IComparable<T>.CompareTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IComparable<T>
的用法示例。
在下文中一共展示了IComparable<T>.CompareTo方法的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);
}
}
}
输出:
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.