int.Parse()和Convert.ToInt32()之间的主要区别是什么? 哪一个是首选
当前回答
TryParse更快…
The first of these functions, Parse, is one that should be familiar to any .Net developer. This function will take a string and attempt to extract an integer out of it and then return the integer. If it runs into something that it can’t parse then it throws a FormatException or if the number is too large an OverflowException. Also, it can throw an ArgumentException if you pass it a null value. TryParse is a new addition to the new .Net 2.0 framework that addresses some issues with the original Parse function. The main difference is that exception handling is very slow, so if TryParse is unable to parse the string it does not throw an exception like Parse does. Instead, it returns a Boolean indicating if it was able to successfully parse a number. So you have to pass into TryParse both the string to be parsed and an Int32 out parameter to fill in. We will use the profiler to examine the speed difference between TryParse and Parse in both cases where the string can be correctly parsed and in cases where the string cannot be correctly parsed. The Convert class contains a series of functions to convert one base class into another. I believe that Convert.ToInt32(string) just checks for a null string (if the string is null it returns zero unlike the Parse) then just calls Int32.Parse(string). I’ll use the profiler to confirm this and to see if using Convert as opposed to Parse has any real effect on performance.
带有示例的源代码
希望这能有所帮助。
其他回答
If you've got a string, and you expect it to always be an integer (say, if some web service is handing you an integer in string format), you'd use Int32.Parse(). If you're collecting input from a user, you'd generally use Int32.TryParse(), since it allows you more fine-grained control over the situation when the user enters invalid input. Convert.ToInt32() takes an object as its argument. (See Chris S's answer for how it works) Convert.ToInt32() also does not throw ArgumentNullException when its argument is null the way Int32.Parse() does. That also means that Convert.ToInt32() is probably a wee bit slower than Int32.Parse(), though in practice, unless you're doing a very large number of iterations in a loop, you'll never notice it.
Convert.ToInt32
有19种重载或者19种不同的调用方式。2010年的版本可能会更多。
它将尝试从以下类型进行转换;
对象,Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64,单个,双精度,十进制,字符串,日期
它还有很多其他的方法;一种方法与数字基础有关,两种方法涉及系统。IFormatProvider
另一方面,Parse只有4种重载或者4种调用方法的不同方式。
Integer.Parse( s As String)
Integer.Parse( s As String, style As System.Globalization.NumberStyles )
Integer.Parse( s As String, provider As System.IFormatProvider )
Integer.Parse( s As String, style As System.Globalization.NumberStyles, provider As System.IFormatProvider )
区别在于:
Int32.Parse()和Int32.TryParse()只能转换字符串。Convert.ToInt32()可以接受任何实现IConvertible的类。如果传递给它一个字符串,那么它们是等效的,除了类型比较等额外开销。如果要转换字符串,那么TryParse()可能是更好的选择。
这是int的一个细节。解析和转换。ToInt32: 比如说,你有一个char数组,char[] a=['1','2','3','4'],你想把每个元素转换成一个整数。 Convert.ToInt32([0])会给你一个数字49。它将其视为ASCII码 int.Parse(a[0])将为您提供正确的输出,即1
如果你有一个字符串数组string[] b=['1','2','3','4'],那么转换。ToInt32和int。Parse在输出上没有区别。两者都返回正确的整数。
转换。ToInt32允许空值,它不会抛出任何错误 Int。解析不允许空值,它会抛出ArgumentNullException错误。
推荐文章
- 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
- 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
- 如何使用JSON确保字符串是有效的JSON。网
- AppSettings从.config文件中获取值
- 通过HttpClient向REST API发布一个空体
- 如何检查IEnumerable是否为空或空?
- 自动化invokerrequired代码模式
- 在c#代码中设置WPF文本框的背景颜色
- 在c#中,什么是单子?
- c#和Java中的泛型有什么不同?和模板在c++ ?
- c#线程安全快速(est)计数器
- 如何将此foreach代码转换为Parallel.ForEach?
- 如何分裂()一个分隔字符串到一个列表<字符串>
- 如何转换列表<字符串>列表<int>?
- c#对象列表,我如何得到一个属性的和