I was working on a C# project which was checking for string, date and integer datatype in given value.
I have 2 methods for checking numeric value.
1st method is converting value to charArray and checking one by one to validate the character using Char.IsNumeric©.
2nd method is validating value using int.TryParse Method.
1st Method
static bool IsNumeric(string value)
{
try
{
char[] chars = value.ToCharArray();
foreach (char c in chars)
{
if (!char.IsNumber(c))
return false;
}
return true;
}
catch (Exception ex) { return false; }
}
2nd Method
static bool IsNumeric(string value)
{
try
{
int number;
bool result = int.TryParse(value, out number);
return result;
}
catch (Exception ex) { return false; }
}
Which one is better?

New Topic/Question
Reply


MultiQuote








|