9

what is the easiest way to check if a typeof() is mathematically usable(numeric).

do i need to use the TryParse method or check it by this:

if (!(DC.DataType == typeof(int) || DC.DataType == typeof(double) || DC.DataType == typeof(long) || DC.DataType == typeof(short) || DC.DataType == typeof(float)))
     {
           MessageBox.Show("Non decimal data cant be calculated");
           return;
     }

if there is a more easy way to achieve this, your free to suggest

Community
  • 1
  • 1
Moonlight
  • 708
  • 1
  • 7
  • 18
  • Related: http://stackoverflow.com/questions/828807/what-is-the-base-class-for-c-sharp-numeric-value-types – Shadow The GPT Wizard Jan 12 '12 at 13:39
  • What does "mathematically usable" mean? Is an array of doubles mathematically usable, for instace? I think it is. – Igor Korkhov Jan 12 '12 at 13:39
  • 3
    possible duplicate of [Using .Net, how can I determine if a type is a Numeric ValueType?](http://stackoverflow.com/questions/124411/using-net-how-can-i-determine-if-a-type-is-a-numeric-valuetype) – Shadow The GPT Wizard Jan 12 '12 at 13:40
  • You can achieve the desired result using that method (If you are checking for a number). Remember int, long, short aren't decimal compatible. – Adam Jan 12 '12 at 13:40
  • @IgorKorkhov after reading the link of ShadowWizard i found out i was meaning numeric types, not decimal, i will edit my post – Moonlight Jan 12 '12 at 13:43

2 Answers2

10

There's nothing much to do, unfortunately. But from C# 3 onwards, you can do something fancier:

public static class NumericTypeExtension
{
    public static bool IsNumeric(this Type dataType)
    {
        if (dataType == null)
            throw new ArgumentNullException("dataType");

        return (dataType == typeof(int)
                || dataType == typeof(double)
                || dataType == typeof(long)
                || dataType == typeof(short)
                || dataType == typeof(float)
                || dataType == typeof(Int16)
                || dataType == typeof(Int32)
                || dataType == typeof(Int64)
                || dataType == typeof(uint)
                || dataType == typeof(UInt16)
                || dataType == typeof(UInt32)
                || dataType == typeof(UInt64)
                || dataType == typeof(sbyte)
                || dataType == typeof(Single)
               );
    }
}

so your original code can be written like this:

if (!DC.DataType.IsNumeric())
{
      MessageBox.Show("Non decimal data cant be calculated");
      return;
}
Moonlight
  • 708
  • 1
  • 7
  • 18
Humberto
  • 7,117
  • 4
  • 31
  • 46
  • these are all the known numeric types? (except char) of are there more? – Moonlight Jan 12 '12 at 13:57
  • No, this is a subset. For a comprehensive list, see [this answer](http://stackoverflow.com/a/5182747/126052) from another topic. I may update my answer later. – Humberto Jan 12 '12 at 14:03
3

You can check for the interfaces that the numeric types implement:

if (data is IConvertible) {
  double value = ((IConvertible)data).ToDouble();
  // do calculations
}

if (data is IComparable) {
  if (((IComparable)data).CompareTo(42) < 0) {
    // less than 42
  }
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005