4

I'm new to C#, coming from Java, and I'd like to check whether a certain object is a Number (it can be an Integer, Double, Float, etc). In Java I did this by saying if (toRet instanceof Number). I'm hoping there's a similar C# thing like if (toRet is Number) but thus far I haven't been able to find a Number class that does this. Is there a way to do this, or do I have to manually check for Integer, Double, etc?

Edit for more info: Basically what I want to do is eventually have a byte array. However, when the array is stored in a text file, the parser I'm using can sometimes think it's an integer array or a double array. In Java, I had this:

JSONArray dblist = (JSONArray)result_;
byte[] reallyToRet = new byte[dblist.size()];
Object toComp = dblist.get(0);

if (toComp instanceof Number)
    for (int i=0; i < dblist.size(); ++i) {
        byte elem = ((Number) dblist.get(i)).byteValue();
        reallyToRet[i] = elem;
    }

    return reallyToRet;
}

The important bit here is the if statement. Sometimes the objects in dblist would parse as integers, sometimes as doubles, and only rarely as bytes, but all I really care about at the end is the byte value.

firechant
  • 886
  • 1
  • 14
  • 22
  • There's not a _direct_ equivalent, but it's certainly not hard to do. Context matters, though, for which solution is best. Can you post some code? – Joel Coehoorn Jul 25 '13 at 15:34
  • Looks like the commonly used solution is to create your own check. http://stackoverflow.com/questions/1130698/checking-if-an-object-is-a-number-in-c-sharp – Jeroen Vannevel Jul 25 '13 at 15:35
  • 3
    There is no need for such a thing in C#, because C# is a strongly typed language, where untyped things (such as having a method with a parameter of type `object`) is not really a good idea to begin with. Post the relevant contextual code where you need such a thing. – Federico Berasategui Jul 25 '13 at 15:35
  • @HighCore: Java is also strongly typed. But in java there's a class `Number` and even classes like `Byte` , `Double`, `Integer`, or `Short` inherit from it. – Tim Schmelter Jul 25 '13 at 15:43
  • @firechant `the parser I'm using can sometimes think it's an integer array or a double array` - Use `File.ReadAllText()` or something and parse the values yourself. You don't need such a thing in .NET. Also, post a sample piece of data (the text file) that you need to read. There's no need for such a `parser` or whatever in C#. – Federico Berasategui Jul 25 '13 at 15:47
  • 3
    Consider `is IConvertible`, an interface implemented by all value type types you'd be interested in. Which you'll need anyway to actually obtain the value. – Hans Passant Jul 25 '13 at 15:55

2 Answers2

10

Well, yeah, but it's an extension method that just ORs all the possibilities.

This is it:

public static bool IsNumber(this object value)
{
    return value is sbyte
            || value is byte
            || value is short
            || value is ushort
            || value is int
            || value is uint
            || value is long
            || value is ulong
            || value is float
            || value is double
            || value is decimal
            || value is BigInteger;
}

and you would use it like this:

if (toRet.IsNumber());

This needs to be in a static class.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
0

I am not sure about any class for that. But you can check for instance, for integer see

int val;
if(Int32.TryParse(integer, out val))  

else

Unlikely, you can use Double.TryParse(number, out val) etc.

usman allam
  • 274
  • 1
  • 5