Int32.TryParse() returns zero on failure - success or failure?

后端 未结 5 1602
萌比男神i
萌比男神i 2021-01-01 09:06

I read this from msdn about Int32.TryParse()

When this method returns, contains the 32-bit signed integer value equivalent to the num

5条回答
  •  礼貌的吻别
    2021-01-01 09:48

    No, TryParse returns true or false to indicate success. The value of the out parameter is used for the parsed value, or 0 on failure. So:

    int value;
    if (Int32.TryParse(someText, out value))
    {
        // Parse successful. value can be any integer
    }
    else
    {
        // Parse failed. value will be 0.
    }
    

    So if you pass in "0", it will execute the first block, whereas if you pass in "bad number" it will execute the second block.

提交回复
热议问题