Friday, October 10, 2014

C# avoid to do try/catch

I am new to C#, today I am facing a issue, the mount of "a First Chance Exception" makes my app running extremely slow. What I did with JAVA style is:


private DateTime convertStringToDateTime(string x)
{
Try
{
DateTime dateX = convert.Datetime(x);
}
catch(FormatException)

{ result null;
}
}

When I run it with debugging, it's keeping throw "a First Chance Exception". I find the solution from one of answers of StackOverFlow. we need try the TryParse

private DateTime convertStringToDateTime(string x)
{
DateTime dateX;
if(DateTime.TryParse(x, out dateX))
{
return dateX;
}
else
{
return null;
}
}

What a good adventure!