Does Int32.TryParse (String, Int32) include an int argument on error?

Of interest, is it possible to assume that if Int32.TryParse(String, Int32)it fails, then the int argument will remain unchanged? For example, if I want my whole to have a default value that is wiser?

int type;
if (!int.TryParse(someString, out type))
    type = 0;

OR

int type = 0;
int.TryParse(someString, out type);
+3
source share
4 answers

The documentation has the answer:

contains a 32-bit signed integer equivalent to the number in s if the conversion was successful, or zero if the conversion failed.

+9
source

TryParsewill install it on 0.

out, .

+7

TryParse 0, - . , .

+2

If it fails, it returns false and sets the type to zero. This would be wiser as a result of:

int type;

if (int.TryParse(someString, out type)) 
  ; // Do something with type
else 
  ; // type is set to zero, do nothing
+1
source

Source: https://habr.com/ru/post/1772412/


All Articles