which version of the code below is right?

Posted by TheVillageIdiot on Stack Overflow See other posts from Stack Overflow or by TheVillageIdiot
Published on 2010-05-29T10:52:30Z Indexed on 2010/05/29 11:02 UTC
Read the original article Hit count: 228

Filed under:
|

Hi I found this function in a utilities code file:

Version 1:

public static bool IsValidLong(string strLong)
{
    bool result = true;
    try
    {
        long tmp = long.Parse(strLong);
    }
    catch (Exception ex)
    {
        result = false;
    }
    return result;
}

I want to replace this (and validators for other types) with following:

Version 2:

public static bool IsValidLong(string strLong)
{
    long l;
    return long.TryParse(strLong, out l);
}

which version is better and why?

© Stack Overflow or respective owner

Related posts about c#

Related posts about best-practices