.net - Convert.Int32() 和 Int32.Parse() 有什么区别?

标签 .net

我在使用 Convert.Int32() 和 int32.Parse() 时有些困惑。
当我们使用 Convert.Int32() 或 int32.Parse()...

最佳答案

Convert.ToInt32() 将尝试将任何东西——无论是字符、 double 、对象,还是你有什么——转换成 Int32。 Int32.Parse() 仅适用于字符串。

编辑:为了回应 OP 的评论,我引用了 from this thread :

Basically the Convert class makes it easier to convert between all the base types.

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException.

It is really a matter of choice whichever you use.

关于.net - Convert.Int32() 和 Int32.Parse() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1500425/

相关文章:

c# - 如何对 CheckedListBox 进行排序

c# - 学习 .NET Web 服务的最佳资源是什么?

.net - 如何以编程方式获取nuget缓存文件夹位置

.net - 当非托管依赖项发生冲突时如何使用 2 个第三方 .net 库?

.net - 用于跟踪已删除记录的删除触发器的替代方案

c# - 在 C# 中开发许可证 - 我从哪里开始?

c# - 寻找在 WPF 中显示数据行的最简单方法

c# - User.Identity.Name 返回 guid

.net - 如何取消单选按钮或复选框选中的更改

.net - |数据目录|返回错误的 AppData 文件夹路径