How to compare two numeric values (SByte, Double) stored as Objects in .NET 2.0?

落爺英雄遲暮 提交于 2019-12-12 06:04:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!