-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathComparers.cs
68 lines (59 loc) · 2.54 KB
/
Comparers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
namespace Algorithms.Common;
public static class Comparers
{
/// <summary>
/// Determines if a specific value is a number.
/// </summary>
/// <returns><c>true</c> if the value is a number; otherwise, <c>false</c>.</returns>
/// <param name="value">Value.</param>
/// <typeparam name="T">The Type of value.</typeparam>
public static bool IsNumber<T>(this T value)
{
if (value is sbyte) return true;
if (value is byte) return true;
if (value is short) return true;
if (value is ushort) return true;
if (value is int) return true;
if (value is uint) return true;
if (value is long) return true;
if (value is ulong) return true;
if (value is float) return true;
if (value is double) return true;
if (value is decimal) return true;
return false;
}
/// <summary>
/// Determines if firstValue is equals to the specified secondValue.
/// </summary>
/// <returns><c>true</c> if firstValue is equals to the specified secondValue; otherwise, <c>false</c>.</returns>
/// <param name="firstValue">The first value.</param>
/// <param name="secondValue">The second value.</param>
/// <typeparam name="T">The Type of values.</typeparam>
public static bool IsEqualTo<T>(this T firstValue, T secondValue) where T : IComparable<T>
{
return firstValue.Equals(secondValue);
}
/// <summary>
/// Determines if thisValue is greater than the specified otherValue.
/// </summary>
/// <returns><c>true</c> if thisValue is greater than the specified otherValue; otherwise, <c>false</c>.</returns>
/// <param name="firstValue">The first value.</param>
/// <param name="secondValue">The second value.</param>
/// <typeparam name="T">The Type of values.</typeparam>
public static bool IsGreaterThan<T>(this T firstValue, T secondValue) where T : IComparable<T>
{
return firstValue.CompareTo(secondValue) > 0;
}
/// <summary>
/// Determines if thisValue is less than the specified otherValue.
/// </summary>
/// <returns><c>true</c> if thisValue is less than the specified otherValue; otherwise, <c>false</c>.</returns>
/// <param name="firstValue">The first value.</param>
/// <param name="secondValue">The second value.</param>
/// <typeparam name="T">The Type of values.</typeparam>
public static bool IsLessThan<T>(this T firstValue, T secondValue) where T : IComparable<T>
{
return firstValue.CompareTo(secondValue) < 0;
}
}