什么是Convert.ToInt32和Int32.Parse之间的区别? [英] What's the difference between Convert.ToInt32 and Int32.Parse?

查看:86
本文介绍了什么是Convert.ToInt32和Int32.Parse之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#那里,你可以同时使用 Int32.Parse Convert.ToInt32 。什么是它们之间的区别?哪个性能更好?什么是我应该在哪里使用场景 Convert.ToInt32 Int32.Parse ,反之亦然?

In C# there you can convert a string to Int32 using both Int32.Parse and Convert.ToInt32. What's the difference between them? Which performs better? What are the scenarios where I should use Convert.ToInt32 over Int32.Parse and vice-versa?

推荐答案

Int32.Parse(字符串s)方法将数字转换到32位有符号整数等效字符串表示。

Int32.parse(string)

Int32.Parse(string s) method converts the string representation of a number to its 32-bit signed integer equivalent.


  • 取值空引用,将 ArgumentNullException

  • 如果取值不是整数,其他值,将 FormatException

  • 取值表示 MINVALUE MaxValue的或更大> 它将发生OverflowException

  • When s is a null reference, it will throw ArgumentNullException.
  • If s is other than integer value, it will throw FormatException.
  • When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

例如对于

string s1 = "1234"; 
string s2 = "1234.65"; 
string s3 = null; 
string s4 = "123456789123456789123456789123456789123456789"; 

int result; 
bool success; 

result = Int32.Parse(s1); //-- 1234 
result = Int32.Parse(s2); //-- FormatException 
result = Int32.Parse(s3); //-- ArgumentNullException 
result = Int32.Parse(s4); //-- OverflowException 






Convert.ToInt32(串)



Convert.ToInt32(字符串s)方法转换的32位有符号整数等效指定的字符串表示。这依次调用 Int32.Parse()方法。


Convert.ToInt32(string)

Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse() method.


  • 取值空引用,它将返回0 ,而不是抛出 ArgumentNullException

  • 如果取值不是整数,其他值,将 FormatException

  • 取值表示的小于许多 MINVALUE 或大于的MaxValue ,将发生OverflowException

  • When s is a null reference, it will return 0 rather than throw ArgumentNullException.
  • If s is other than integer value, it will throw FormatException.
  • When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

例如:

result = Convert.ToInt32(s1); //-- 1234 
result = Convert.ToInt32(s2); //-- FormatException 
result = Convert.ToInt32(s3); //-- 0 
result = Convert.ToInt32(s4); //-- OverflowException 






Int32.TryParse(字符串,OUT INT)



Int32.Parse(字符串,OUT INT)方法转换的32位有符号整数相当于out变量指定的字符串表示,如果它被成功解析,否则为false返回true。这种方法是在C#2.0中可用。


Int32.TryParse(string, out int)

Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise. This method is available in C# 2.0.


  • 取值空引用,将返回0 ,而不是抛出 ArgumentNullException

  • 如果取值不是整数其他值,输出变量的有0 ,而不是 FormatException

  • 取值表示 MINVALUE 少一个数字或更大比 MaxValue的 ,输出变量将有0 而非发生OverflowException

  • When s is a null reference, it will return 0 rather than throw ArgumentNullException.
  • If s is other than an integer value, the out variable will have 0 rather than FormatException.
  • When s represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException.

例如:

success = Int32.TryParse(s1, out result); //-- success => true; result => 1234 
success = Int32.TryParse(s2, out result); //-- success => false; result => 0 
success = Int32.TryParse(s3, out result); //-- success => false; result => 0 
success = Int32.TryParse(s4, out result); //-- success => false; result => 0 






结论



Convert.ToInt32() Int32.Parse(),因为它返回0更好而不是例外。但同样,根据需要,这可以被使用。 的TryParse 将是最好的,因为它总是自行处理异常。


Conclusion

Convert.ToInt32() is better than Int32.Parse() since it returns 0 rather than an exception. But again, according to the requirement, this can be used. TryParse will be the best since it always handles exceptions by itself.

这篇关于什么是Convert.ToInt32和Int32.Parse之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆