BackDoorNoBaby BackDoorNoBaby - 3 months ago 19
C# Question

Easy way to check if primitive data type "A" can fit safely into primitive data type "B" in c# (Assuming both are the same signedness)

Given this function:

bool WillDataTypeFit(Type typeToCheck, Type typeToFitInto)
{
bool bWillFit = false;

// Psedocode
// IF typeToCheck is NOT the same as typeToFitInfo
// IF typeToCheck && typeToFitInfo are signed
// Check for signed fit
// ELSE IF typeToCheck && typeToFitInfo are unsigned
// Check for unsigned fit
// ELSE
// bWillFit = true;

return (bWillFit);
}


Is there a simple way to see if
typeToCheck
can fit safely into
typeToFitInto
?

I am concerned with primitive data types, excluding
DateSpan
,
DateTime
,
bool
,
decimal
,
string
,
char
,
object


So basically
signed/unsigned int
and
float
and
double


EDIT:

"Fit into" meaning I can safely convert the max/min value of
typeToCheck
to type of
typeToFitInto
, however at the time of this check I don't have any value to convert, so I am trying to simply check the data types

EDIT2:

I've realized that this question is generally flawed, since without a value you can't guarantee something like a positive
sbyte
fitting into a
byte
, I edited the question so the check is only evaluated if both data types have the same signedness

Answer

No easy way to do it that I know of. You will need something like this:

bool WillDataTypeFit(Type typeToCheck, Type typeToFitInto)
{
    if (typeToCheck.Equals(typeToFitInto))
    {
        return true;
    }
    else if (typeToCheck == typeof(sbyte))
    {
        if (typeToFitInto == typeof(short))
            return true;
        else if (typeToFitInto == typeof(int))
            return true;
        else if (typeToFitInto == typeof(long))
            return true;
        else if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(byte))
    {
        if (typeToFitInto == typeof(short))
            return true;
        else if (typeToFitInto == typeof(ushort))
            return true;
        else if (typeToFitInto == typeof(int))
            return true;
        else if (typeToFitInto == typeof(uint))
            return true;
        else if (typeToFitInto == typeof(long))
            return true;
        else if (typeToFitInto == typeof(ulong))
            return true;
        else if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(ushort))
    {
        if (typeToFitInto == typeof(int))
            return true;
        else if (typeToFitInto == typeof(uint))
            return true;
        else if (typeToFitInto == typeof(long))
            return true;
        else if (typeToFitInto == typeof(ulong))
            return true;
        else if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(short))
    {
        if (typeToFitInto == typeof(int))
            return true;
        else if (typeToFitInto == typeof(long))
            return true;
        else if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(uint))
    {
        if (typeToFitInto == typeof(long))
            return true;
        else if (typeToFitInto == typeof(ulong))
            return true;
        else if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(int))
    {
        if (typeToFitInto == typeof(long))
            return true;
        else if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(long) || typeToCheck == typeof(ulong))
    {
        if (typeToFitInto == typeof(float))
            return true;
        else if (typeToFitInto == typeof(double))
            return true;
        else if (typeToFitInto == typeof(decimal))
            return true;
        else
            return false;
    }
    else if (typeToCheck == typeof(float))
    {
        if (typeToFitInto == typeof(double))
            return true;
        else
            return false;
    }
    else
    {
        return false;
    }
}

I believe this is exhaustive for number types, but you can check here Implicit Numeric Conversion