/ Generic namespace GregoryAdam.Base.ExtensionMethods { public static partial class ExtensionMethods { #region Between /// <summary> /// tells whether s between s1 and s2 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="s"></param> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns>true or false</returns> // // IComparable<T> // - By definition, any object compares greater than a null reference // - two null references compare equal to each other public static bool Between<T>(this T s, T s1, T s2) where T : IComparable<T> { if (s != null) return (s.CompareTo(s1) >= 0) && (s.CompareTo(s2) <= 0); // s == null here /* * s1 s s2 s1<=s s<=s2 final * !=null null !null false true false * null null !null true true true * * !null null null false true false * null null null true true true * */ /* * conclusion * It does not matter whether s2 is null or not * s < non-null-s2 * s == null-s2 * hence always: s <= s2 * * the only thing that matters is s1 * non_null_s1 < s * null_s1 == s */ return (s1 == null); } #if false // 3 times slower public static bool Between(this IComparable s, IComparable s1, IComparable s2) { return (s.CompareTo(s1) >= 0) && (s.CompareTo(s2) <= 0); } #endif #endregion //-------------------------------------------------------------------------------- //-------------------------------------------------------------------------------- #region InList // no problems with null references public static bool InList<T>(this T s, params T[] list) where T : IEquatable<T> { return list.Contains(s); } #endregion //-------------------------------------------------------------------------------- } }