c# - Int32.Parse() VS Convert.ToInt32()?

标签 c#

intID1 = Int32.Parse(myValue.ToString());
intID2 = Convert.ToInt32(myValue);

哪个更好,为什么?

最佳答案

它们完全相同,只是 Convert.ToInt32(null) 返回 0

Convert.ToInt32 定义如下:

    public static int ToInt32(String value) {
        if (value == null) 
            return 0;
        return Int32.Parse(value, CultureInfo.CurrentCulture);
    }

关于c# - Int32.Parse() VS Convert.ToInt32()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3713970/

相关文章:

c# - {"Cannot insert explicit value for identity column in table ' Cantact' 当 IDENTITY_INSERT 设置为 OFF 时。"}

c# - 如何在特定的 jwt 上下文下发出 post 请求 webclient

c# - 如何使用 GSON 将 JSON 值映射到不同的类

c# - 从公式字符串中解析出 Excel 函数

c# - Microsoft.Practices.TransientFaultHandling.RetryPolicy 的正确代码是什么?

c# - 如何将复杂的 XML 转换为 .NET 类?

c# - 从类名中获取模型元数据

c# - 在 .Net、WinForms 中在运行时添加用户控件缩放时出现问题

c# - 无法为 webservice.asmx 创建类型

c# - 检查 XML 中是否存在元素