1

Hmm, this may seems a simple question, but I would like to know the fastest way.

I have an object which is a boxed number, I don't know is it double, int, or some numeric type. I want to check if the boxed value is negative.

I use this conversion but I am not sure if it is as fast as can be:

private bool CheckNegative(object number)
{
   return System.Convert.ToDouble(number) < 0;
}

Arsen

Jon
  • 428,835
  • 81
  • 738
  • 806
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • Why do you need this code in the first place? This looks like a bad software design. – Konrad Rudolph Aug 02 '12 at 13:31
  • possible duplicate of [How do I check if a number is positive or negative in c#?](http://stackoverflow.com/questions/4099366/how-do-i-check-if-a-number-is-positive-or-negative-in-c) – Garrett Vlieger Aug 02 '12 at 13:32
  • 4
    @GarrettVlieger: Obviously not a dupe, the number is boxed here. – Jon Aug 02 '12 at 13:33
  • 3
    If you need it to be fast, know the type of the value. – Jodrell Aug 02 '12 at 13:39
  • Question : How did u get that much points without knowing such a simple solution ? Use Baseclass 'Number' and check if your object is derived of this – Mohnkuchenzentrale Aug 02 '12 at 13:42
  • 2
    I am sure there isn't any such class called Number that double, int, etc derives from. – ata Aug 02 '12 at 13:44
  • http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.compilerservices.operators.compareobjectless.aspx – Hans Passant Aug 02 '12 at 13:48
  • @HansPassant: "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code." - that probably means it might disappear without notice in any future version. – O. R. Mapper Aug 02 '12 at 13:48
  • @KonradRudolph it is WPV convertion method, I need to check hilight negative values, I want to use this converter for all numeric types, I don't think it is bad design result here.. – Arsen Mkrtchyan Aug 02 '12 at 20:45
  • @ArsenMkrt I have to admit that I don’t know what WPV is but since this code completely subverts type checking I can say that in almost all situations it’s bad code. If you want to compare arbitrary objects, use the (generic!) `IComparable` or `IComparer` interfaces. – Konrad Rudolph Aug 02 '12 at 20:52
  • Thanks for advice @KonradRudolph, here is wpf converter interface http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx, I think it is better to have one converter for all numeric types than to write for each type... – Arsen Mkrtchyan Aug 02 '12 at 21:06

4 Answers4

5

Use the C# version 4 dynamic keyword here to take advantage of its dynamic operator binding:

private static bool CheckNegative(dynamic number)
{
   return number < 0;
}

Works fine on boxed value types as well.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

It seems like the only way to do it.

But if you wanted to avoid writing multiple functions that take double, int, float, etc to check for negative number, I would redirect you to following post: https://stackoverflow.com/a/828820/150830

Community
  • 1
  • 1
ata
  • 8,853
  • 8
  • 42
  • 68
1

I imagine that this might be faster because it cuts to the chase and does not need to convert integral types to IEEE floating point:

bool CheckNegative(object number)
{
    switch(Type.GetTypeCode(number.GetType())) {
        case TypeCode.Int32:
            return (int)number < 0;
        case TypeCode.Single:
            return (float)number < 0;
        // etc etc
        default:
            throw new ArgumentException();
    }
}

It won't be pretty though.

Update: Turns out this approach is slightly slower, except when dealing with decimal. So the best practical decision seems to be keep Convert.ToDouble.

Jon
  • 428,835
  • 81
  • 738
  • 806
-4
object bla = 2343;

if ( bla is Number )
{
     bool t = (Number)bla > 0;
}
sloth
  • 99,095
  • 21
  • 171
  • 219
Mohnkuchenzentrale
  • 5,745
  • 4
  • 30
  • 41