本文整理汇总了C#中this.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# this.CompareTo方法的具体用法?C# this.CompareTo怎么用?C# this.CompareTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类this
的用法示例。
在下文中一共展示了this.CompareTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompareTo
/// <summary>
/// Compares two doubles and determines which double is bigger.
/// a < b -> -1; a ~= b (almost equal according to parameter) -> 0; a > b -> +1.
/// </summary>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="maximumAbsoluteError">The absolute accuracy required for being almost equal.</param>
public static int CompareTo(this double a, double b, double maximumAbsoluteError)
{
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (double.IsNaN(a) || double.IsNaN(b))
{
return a.CompareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a.CompareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (AlmostEqual(a, b, maximumAbsoluteError))
{
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.CompareTo(b);
}
示例2: ThrowIfOutOfRange
/// <summary>
/// 如果 值 小于 min 或 大于 max 则引发 ArgumentOutOfRangeException 异常
/// </summary>
/// <param name="value">参数值</param>
/// <param name="argumentName">参数名称</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
public static void ThrowIfOutOfRange(this IComparable value, string argumentName, object min, object max)
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
throw new ArgumentOutOfRangeException(argumentName);
}
}
示例3: ValueCheck
/// <summary>
/// 校验double值的有效性,如果返回false,这个值不可用,不能参与计算。
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool ValueCheck(this double value)
{
if (double.IsNaN(value) || double.IsInfinity(value) || value.CompareTo(double.Epsilon) == 0 ||
value.CompareTo(double.MaxValue) == 0 || value.CompareTo(double.MinValue) == 0)
{
return false;
}
else
{
return true;
}
}
示例4: EnhancedCompareTo
public static bool EnhancedCompareTo(this string itemValue, Criteria criteria)
{
switch (criteria.Operator)
{
case Operators.Equal:
return itemValue.CompareTo(criteria.Value) == 0;
case Operators.NotEqual:
return itemValue.CompareTo(criteria.Value) != 0;
case Operators.StartsWith:
return itemValue.StartsWith(criteria.Value);
case Operators.Contains:
return itemValue.Contains(criteria.Value);
}
return false;
}
示例5: IsAssignableFrom
public static bool IsAssignableFrom(this TypeReference baseType, TypeReference type, Action<string> logger = null)
{
if (type.IsGenericParameter)
return baseType.CompareTo(type);
return baseType.Resolve().IsAssignableFrom(type.Resolve(), logger);
}
示例6: IsWithinXDays
/// <summary>
/// Returns true if the DateTime is within X number of days, false otherwise.
/// </summary>
/// <param name="dt"></param>
/// <param name="days"></param>
/// <param name="useUtc"></param>
/// <returns></returns>
public static bool IsWithinXDays(this DateTime dt, int days, bool useUtc = true)
{
DateTime past = DateTime.Today.AddDays(-days).ToUniversalTime();
if (!useUtc) past = DateTime.Today.AddDays(-days);
return dt.CompareTo(past) >= 0;
}
示例7: IsGreater
/// <summary>
/// Indicates whether the instance is greater as the reference value.
/// </summary>
/// <param name="value">The instance to test.</param>
/// <param name="referenceValue">The reference value to test.</param>
/// <returns><strong>true</strong> if the instance is greater as the reference value;
/// otherwise, <strong>false</strong>.</returns>
/// <exception cref="ArgumentNullException"><em>value</em> or <em>referenceValue</em> is
/// <strong>null</strong>.</exception>
public static bool IsGreater(this IComparable value, IComparable referenceValue)
{
Precondition.IsNotNull(value, nameof(value));
Precondition.IsNotNull(referenceValue, nameof(referenceValue));
return value.CompareTo(referenceValue) == 1;
}
示例8: SafeCompareTo
internal static int SafeCompareTo(this decimal val, decimal? other)
{
if (other == null)
{
return -1;//treat null as less
}
int compare = val.CompareTo(other.Value);
return compare;
}
示例9: IsBigger
public static bool IsBigger(this IComparable object1, IComparable object2)
{
if (object1 == null)
{
return false; // null is not bigger than anything
}
return object1.CompareTo(object2) > 0;
}
示例10: IsSmaller
public static bool IsSmaller(this IComparable object1, IComparable object2)
{
if (object1 == null)
{
return object2 != null; // smaller than anything but null
}
return object1.CompareTo(object2) < 0;
}
示例11: CompareUpdate
/// <summary>
/// Compares string builder with text,
/// when it's different, Clears string builder and Appends text.
/// Returns true when original string builder was modified.
/// </summary>
public static bool CompareUpdate(this StringBuilder sb, StringBuilder text)
{
if (sb.CompareTo(text) != 0)
{
sb.Clear();
sb.AppendStringBuilder(text);
return true;
}
return false;
}
示例12: CompareIgnoreNullTo
/// <summary>
/// Compara dos valores
/// </summary>
public static int CompareIgnoreNullTo(this string strFirst, string strSecond)
{
if (string.IsNullOrEmpty(strFirst) && string.IsNullOrEmpty(strSecond))
return 0;
else if (string.IsNullOrEmpty(strFirst) && !string.IsNullOrEmpty(strSecond))
return -1;
else if (!string.IsNullOrEmpty(strFirst) && string.IsNullOrEmpty(strSecond))
return 1;
else
return strFirst.CompareTo(strSecond);
}
示例13: NearlyEquals
public static bool NearlyEquals(this float a, float b)
{
float absA = Math.Abs(a);
float absB = Math.Abs(b);
float diff = Math.Abs(a - b);
if (a.CompareTo(b) == 0)
{ // shortcut, handles infinities
return true;
}
else if (a.CompareTo(0f) == 0 || b.CompareTo(0f) == 0 || diff < float.MinValue)
{
// a or b is zero or both are extremely close to it
// relative error is less meaningful here
return diff < (float.Epsilon * float.MinValue);
}
else
{ // use relative error
return diff / Math.Min((absA + absB), float.MaxValue) < float.Epsilon;
}
}
示例14: Between
/// <summary>
/// 対象日が期間内かどうかのチェック
/// </summary>
/// <param name="date">対象日</param>
/// <param name="longFrom">期間開始日</param>
/// <param name="longTo">期間終了日</param>
/// <returns>bool</returns>
public static bool Between(this DateTime date, DateTime longFrom, DateTime longTo)
{
if (longTo.CompareTo(longFrom) < 0)
{
return false;
}
if (date.CompareTo(longFrom) < 0)
{
return false;
}
if (longTo.CompareTo(date) < 0)
{
return false;
}
return true;
}
示例15: CompareTo_
/// <summary>以指定的比较类型比较指定日期早于、等于或晚于当前指定的日期</summary>
/// <param name="t1">当前指定的日期</param>
/// <param name="t2">要比较的日期</param>
/// <param name="compareType">比较类型</param>
/// <returns>以 -1、0、1 表示的早于、等于或晚于当前指定的日期</returns>
public static int CompareTo_(this DateTime t1, DateTime t2, DateTimeCompareType compareType = DateTimeCompareType.None)
{
if(compareType == DateTimeCompareType.None)
return t1.CompareTo(t2);
DateTime d1, d2;
int y1 = t1.Year, y2 = t2.Year;
switch(compareType)
{
case DateTimeCompareType.ByYear:
d1 = new DateTime(y1, 1, 1);
d2 = new DateTime(y2, 1, 1);
break;
case DateTimeCompareType.ByHalfYear:
d1 = new DateTime(y1, t1.GetHalfYear_(), 1);
d2 = new DateTime(y2, t2.GetHalfYear_(), 1);
break;
case DateTimeCompareType.ByQuarter:
d1 = new DateTime(y1, t1.GetQuarter_(), 1);
d2 = new DateTime(y2, t2.GetQuarter_(), 1);
break;
case DateTimeCompareType.ByMonth:
d1 = new DateTime(y1, t1.Month, 1);
d2 = new DateTime(y2, t2.Month, 1);
break;
case DateTimeCompareType.ByWeek:
if(y1 != y2)
return y1.CompareTo(y2);
return t1.GetWeekOfYear_().CompareTo(t2.GetWeekOfYear_());
case DateTimeCompareType.ByDay:
return t1.Date.CompareTo(t2.Date);
case DateTimeCompareType.ByHour:
d1 = new DateTime(y1, t1.Month, t1.Day, t1.Hour, 0, 0);
d2 = new DateTime(y2, t2.Month, t2.Day, t2.Hour, 0, 0);
break;
case DateTimeCompareType.ByMinute:
d1 = new DateTime(y1, t1.Month, t1.Day, t1.Hour, t1.Minute, 0);
d2 = new DateTime(y2, t2.Month, t2.Day, t2.Hour, t2.Minute, 0);
break;
default:
d1 = new DateTime(y1, t1.Month, t1.Day, t1.Hour, t1.Minute, t1.Second);
d2 = new DateTime(y2, t2.Month, t2.Day, t2.Hour, t2.Minute, t2.Second);
break;
}
return d1.CompareTo(d2);
}