how can I test if a dynamic variable is a double for example?
I need to do something like:
void someMethod(dynamic var1)
{
if(var1.isDouble)
{...
}else if(var1 is int)
// do something else....
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That approach is fine (i.e.
var1 is double), though that’s usually not whatdynamicis meant to accomplish. More often, you shoulddynamicwhen you do know what the type will be, but it’s difficult or impossible to show that at compile-time (e.g. a COM interop scenario, aViewBagin MVC, etc.) You could just useobjectif you want to pass a variable of unknown type. Otherwise, the run-time will do the type analysis for you during execution, which can be a big performance hit if that’s not what you need.In general, there could be scenarios where you’d want to use
dynamicas a catch-all container, but this doesn’t appear to be one of them. In this case, why not have a number of method overloads that each take the desired type: