If-statement GetType() c#
if (numerator is int) { ... }
or
if (numerator.GetType() == typeof(int)) {...}
The former is usually better.
EDIT:
Нou say the problem is parsing numbers from string representation. I'm afraid, the best approach here is to call type.TryParse
and check if given string can be parsed as a number of given type.
E.g.
var tokens = line.Split('/');
double dArg1,dArg2; int iArg1, iArg2;
if (int.TryParse(tokens[0], out iArg1)
&& int.TryParse(tokens[1], out iArg2)){
return iArg1/iArg2;
} else if (double.TryParse(tokens[0], out dArg1)
&& double.TryParse(tokens[1], out dArg2)){
return dArg1/dArg2;
} else { /* handle error */ }
Note that all int
s can be parsed as double
s, so you need to try to parse token as int
before trying to parse it as `double.
if (numerator.GetType() == typeof(int))
{
...
}
typeof (MSDN)
You can use the typeof-operator:
if(typeof(int) == numerator.GetType())
{
//put code here
}