#1,011 – TryParse Indicates Whether a Parse Operation Will Succeed

You can use the Parse method to convert a string that represents a number to its equivalent numerical representation.

            string numberString = "108";
            int number = int.Parse(numberString);

If the string does not represent a value of the associated numeric type, or represents a numeric value that is outside the range of the type, a FormatException or OverflowException is generated.

            int n1 = int.Parse("3.4");    // FormatException

You can avoid the exception by using the TryParse method.  If the parse operation succeeds, the parsed value is stored in the output parameter and TryParse returns true.  If the parse operation does not succeed, the output parameter is not written to and TryParse returns false.  No exception is thrown.

            int n1;
            if (int.TryParse("3.4", out n1))
                Console.WriteLine("Parse worked--n1 contains number");
                Console.WriteLine("Can't parse");


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #1,011 – TryParse Indicates Whether a Parse Operation Will Succeed

  1. Pingback: TryParse - The Daily Six Pack: January 17, 2014

  2. Jeffry Hardy says:

    The bottom line is, at least for me, “TryParse” does not work the way that I intuitively thought that it would the first time I heard about it. Nevertheless, it is very useful in all kinds of situations and allows you to gracefully parse a string into another data type without crashing your entire application in the process when the input was not in the correct format (which is the subject of another post as well).

  3. Peter says:

    f the parse operation does not succeed, the output parameter is unaffected and TryParse returns false.

    This is generally not true:
    http://msdn.microsoft.com/en-us/library/aa645764(v=vs.71).aspx states
    “Every output parameter of a method must be definitely assigned before the method returns.”

    By convention (for TryParse Methods, at least) this is the default value for the type, so for integer 0, although there is no formal guarantee this is the case. So taking your example and making two simple modifications we can see this at work:

    int n1 = 50;
    if (int.TryParse(“3.4”, out n1))
    Console.WriteLine(“Parse worked–n1 contains number”);
    Console.WriteLine(“Can’t parse”);


    this gives an output of:
    Can’t parse

    So despite the fact that we input the int as 50 it is being modified to 0 within the int.TryParse. You can actually see this when you decompile the TryParse method too, I wont paste that here cause it is fairly verbose.

    This is quite important to know as it could result in a bug if you didnt want to overwrite some “default” value or state if a parse failed.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: