Int32.TryParse(String,Int32)是否在失败时改变了int参数?

Ant*_*ony 3 .net tryparse

出于兴趣,可以安全地假设如果Int32.TryParse(String, Int32)失败,那么int参数将保持不变?例如,如果我希望我的整数具有默认值,哪个更明智?

int type;
if (!int.TryParse(someString, out type))
    type = 0;
Run Code Online (Sandbox Code Playgroud)

要么

int type = 0;
int.TryParse(someString, out type);
Run Code Online (Sandbox Code Playgroud)

Joe*_*orn 9

文件给出了答案:

如果转换成功,则包含等效于s中包含的数字的32位有符号整数值,如果转换失败,则包含0.


SLa*_*aks 7

TryParse将它设置为0.

由于它是一个out参数,因此即使在失败时也不可能在没有设置值的情况下返回.