C# - how to determine whether a Type is a number

后端 未结 18 2309
傲寒
傲寒 2020-11-28 22:47

Is there a way to determine whether or not a given .Net Type is a number? For example: System.UInt32/UInt16/Double are all numbers. I want to avoid a long switc

18条回答
  •  忘掉有多难
    2020-11-28 23:28

    This may work as well. However, you may want to follow it up with a Type.Parse to cast it the way you want it afterwards.

    public bool IsNumeric(object value)
    {
        float testValue;
        return float.TryParse(value.ToString(), out testValue);
    }
    

提交回复
热议问题