I'm having a weird issue.
I'm receiving "Cannot cast from source type to destination type"-exception when trying to cast from double to float, but I can't understand why and why this (see below) works any different.
//this throws exception
void doSomething1(object val)
{
if(val is double || val is float)
{
doSomethingElse((float)val);
}
}
//this works
void doSomething2(object val)
{
if(val is double || val is float)
{
double tmpDouble = (double)val;
float tmpFloat = (float)tmpDouble;
doSomethingElse(tmpFloat);
}
}
void doSomethingElse(float val)
{
//do something
}
I've checked the Type
of val
and it is System.Double
, so what is wrong here?
EDIT: Someone flagged this has duplicate (which it kinda is), but the exception I received is not the same as the one received in the "duplicate"-post and thus I could not locate this exact problem (especially since I was stupid enough to not understand it was an (un)boxing issue).