public static void Main()
var objS = new NullableRangePair<string>(s1, s2);
Console.WriteLine("0." + objS);
var objN = new NullableRangePair<int?>(v1, v2);
Console.WriteLine("9." + objN);
public class NullableRangePair<T>
public T First { get; private set; }
public T Second { get; private set; }
public NullableRangePair(T first, T second)
throw new Exception("傳入的型別必需是 Nullable 型別");
if (first == null || second == null)
Type t2 = Nullable.GetUnderlyingType(t1);
var query = t2.GetInterfaces().Where(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(IComparable<>)
&& i.GetGenericArguments().Length == 1
&& i.GetGenericArguments()[0] == t2)
throw new Exception("Nullable<T> 的型別必需實作IComparable<T>");
int result = (value1 as IComparable).CompareTo(value2);
public override string ToString()
return string.Format("{0} ~ {1}", First, Second);