Friday, June 20, 2008

IsNumber IsNaN in C# .NET

Many times we get the requirement to find out where the input is a valid integer or not. In C#, there is no straight forward method for this. Although many basic languages like JavaScript provide IsNaN() function for the same purpose. Anyway, I will show how to suffice this requirement writing minimum amount of code possible

The magic code to do this is

int dummyInt;
if(Int32.TryParse(months,out dummyInt))
return "Yes, it is integer";
else
return "No, it is not an integer";


Some people use Int32.Parse and use try-catch block. But that option is not as quick as this.

Technorati Tags: ,


IceRocket Tags: ,

No comments: