|
|
|
@ -0,0 +1,35 @@ |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Text; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
|
|
namespace EffectiveCSharp.UseGeneric |
|
|
|
|
{ |
|
|
|
|
internal class GenericCompatible |
|
|
|
|
{ |
|
|
|
|
public static bool AreEqual<T>(T left, T right) |
|
|
|
|
{ |
|
|
|
|
if (left == null) |
|
|
|
|
return right == null; |
|
|
|
|
|
|
|
|
|
if (left is IComparable<T>) |
|
|
|
|
{ |
|
|
|
|
IComparable<T> lval = left as IComparable<T>; |
|
|
|
|
if (right is IComparable<T>) |
|
|
|
|
return lval.CompareTo(right) == 0; |
|
|
|
|
else |
|
|
|
|
throw new ArgumentException("Type does not implement IComparable<T>", nameof(right)); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
throw new ArgumentException("Type does not implement IComparable<T>", nameof(left)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static bool BetterAreEqual<T>(T left, T right) where T : IComparable<T> |
|
|
|
|
{ |
|
|
|
|
return left.CompareTo(right) == 0; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |