How to find out if a numeric type is signed or unsigned in C#

Viewed 711

I want to know details about the type of a field by reflection.

I know I can find out that it is a value type with Type.IsValueType. But from there how do I know it is a number? A fixed point number? Signed or unsigned??

Is there anything like Type.IsSigned?

1 Answers

There aren't that many numeric types that are unsigned, so why not compose a list of that:

if (new Type[] { typeof(ushort), typeof(uint), typeof(ulong), typeof(byte) }.Contains(type))
{
    // unsigned.
}

Or if you just want to compare the value (here o):

if (o is ushort || o is uint || o is ulong || o is byte)
{
    // unsigned.
}
Related