Is there a try Convert.ToInt32… avoiding exceptions

前端 未结 7 1902
长发绾君心
长发绾君心 2020-12-08 18:51

I\'d like to know if there is a \"safe\" way to convert an object to an int, avoiding exceptions.

I\'m looking for something like public static bo

7条回答
  •  生来不讨喜
    2020-12-08 19:04

    int variable = 0;
    int.TryParse(stringValue, out variable);
    

    If it can't be parsed, the variable will be 0. See http://msdn.microsoft.com/en-us/library/f02979c7.aspx

提交回复
热议问题