问题
In my custom sorting algorithm, I need to compare numeric types stored as Objects in an array. We can have SByte, Double, Int32, etc values mixed in one array. How can I compare two values of that array in my IComparer implementation?
An example. Let's say we have two numeric values:
object var1 = (sbyte)-8;
object var2 = (double)123.456;
It would be nice if code like the following one worked, but it fails:
IComparable cmp1 = var1 as IComparable;
IComparable cmp2 = var2 as IComparable;
MessageBox.Show(cmp1.CompareTo(cmp2).ToString());
How can I compare two boxed numeric values in .NET 2.0?
来源:https://stackoverflow.com/questions/28986745/how-to-compare-two-numeric-values-sbyte-double-stored-as-objects-in-net-2-0