-
int.Parse()
和Convert.ToInt32()
之间的主要区别是什么? - 哪一个是首选
#1楼
Convert.ToInt32
有19种重载或19种不同的方式可以称之为。 2010版可能更多。
它将尝试从以下TYPES转换;
Object,Boolean,Char,SByte,Byte,Int16,UInt16,Int32,UInt32,Int64,UInt64,Single,Double,Decimal,String,Date
它还有许多其他方法; 一个与数字基数有关,两个方法涉及System.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 )
#2楼
为澄清打开控制台应用程序,只需复制下面的代码并将其粘贴到static void Main(string[] args)
方法中,希望你能理解
public class Program
{
static void Main(string[] args)
{
int result;
bool status;
string s1 = "12345";
Console.WriteLine("input1:12345");
string s2 = "1234.45";
Console.WriteLine("input2:1234.45");
string s3 = null;
Console.WriteLine("input3:null");
string s4 = "1234567899012345677890123456789012345667890";
Console.WriteLine("input4:1234567899012345677890123456789012345667890");
string s5 = string.Empty;
Console.WriteLine("input5:String.Empty");
Console.WriteLine();
Console.WriteLine("--------Int.Parse Methods Outputs-------------");
try
{
result = int.Parse(s1);
Console.WriteLine("OutPut1:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut1:"+ee.Message);
}
try
{
result = int.Parse(s2);
Console.WriteLine("OutPut2:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut2:" + ee.Message);
}
try
{
result = int.Parse(s3);
Console.WriteLine("OutPut3:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut3:" + ee.Message);
}
try
{
result = int.Parse(s4);
Console.WriteLine("OutPut4:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut4:" + ee.Message);
}
try
{
result = int.Parse(s5);
Console.WriteLine("OutPut5:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut5:" + ee.Message);
}
Console.WriteLine();
Console.WriteLine("--------Convert.To.Int32 Method Outputs-------------");
try
{
result= Convert.ToInt32(s1);
Console.WriteLine("OutPut1:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut1:" + ee.Message);
}
try
{
result = Convert.ToInt32(s2);
Console.WriteLine("OutPut2:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut2:" + ee.Message);
}
try
{
result = Convert.ToInt32(s3);
Console.WriteLine("OutPut3:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut3:" + ee.Message);
}
try
{
result = Convert.ToInt32(s4);
Console.WriteLine("OutPut4:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut4:" + ee.Message);
}
try
{
result = Convert.ToInt32(s5);
Console.WriteLine("OutPut5:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut5:" + ee.Message);
}
Console.WriteLine();
Console.WriteLine("--------TryParse Methods Outputs-------------");
try
{
status = int.TryParse(s1, out result);
Console.WriteLine("OutPut1:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut1:" + ee.Message);
}
try
{
status = int.TryParse(s2, out result);
Console.WriteLine("OutPut2:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut2:" + ee.Message);
}
try
{
status = int.TryParse(s3, out result);
Console.WriteLine("OutPut3:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut3:" + ee.Message);
}
try
{
status = int.TryParse(s4, out result);
Console.WriteLine("OutPut4:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut4:" + ee.Message);
}
try
{
status = int.TryParse(s5, out result);
Console.WriteLine("OutPut5:" + result);
}
catch (Exception ee)
{
Console.WriteLine("OutPut5:" + ee.Message);
}
Console.Read();
}
}
#3楼
这取决于参数类型。 例如,我今天刚刚发现它将使用ASCII值将char直接转换为int。 不完全是我想要的功能......
你被警告了!
public static int ToInt32(char value)
{
return (int)value;
}
Convert.ToInt32('1'); // Returns 49
int.Parse('1'); // Returns 1
#4楼
没有差别。 Convert.ToInt32()
int.Parse()
内部调用int.Parse()
除了一件事,当参数为null
时, Convert.ToInt32()
返回0
否则两者都以相同的方式工作
#5楼
如果你有一个字符串,并且你希望它总是一个整数(比如说,如果某个Web服务正在以字符串格式处理一个整数),你将使用
Int32.Parse()
。如果您正在收集用户的输入,则通常使用
Int32.TryParse()
,因为它允许您在用户输入无效输入时对情况进行更细粒度的控制。Convert.ToInt32()
接受一个对象作为其参数。 (参见Chris S的答案,了解它的工作原理)Convert.ToInt32()
在其参数为Int32.Parse()
的方式为null时也不会抛出ArgumentNullException
。 这也意味着Convert.ToInt32()
可能比Int32.Parse()
慢一点,但在实践中,除非你在循环中进行大量的迭代,否则你永远不会注意到它。
#6楼
TryParse更快......
第一个这样的函数Parse是任何.Net开发人员都应该熟悉的函数。 此函数将接受一个字符串并尝试从中提取整数,然后返回整数。 如果它遇到无法解析的内容,则会抛出FormatException,或者如果数字太大而导致OverflowException。 此外,如果您传递一个空值,它可以抛出ArgumentException。
TryParse是新的.Net 2.0框架的新增功能,它解决了原始Parse函数的一些问题。 主要区别在于异常处理非常慢,所以如果TryParse无法解析字符串,它就不像Parse那样抛出异常。 相反,它返回一个布尔值,指示它是否能够成功解析数字。 因此,您必须将要解析的字符串和要填充的Int32输出参数传递给TryParse。我们将使用探查器检查TryParse和Parse之间的速度差异,在这两种情况下,可以正确解析字符串,以及在何种情况下无法正确解析字符串。
Convert类包含一系列将一个基类转换为另一个基类的函数。 我相信Convert.ToInt32(字符串)只是检查一个空字符串(如果字符串为null,它与Parse不同,它返回零)然后只调用Int32.Parse(字符串)。 我将使用分析器来确认这一点,看看使用Convert而不是Parse对性能有任何实际影响。
希望这可以帮助。
#7楼
区别在于:
Int32.Parse()
和Int32.TryParse()
只能转换字符串。 Convert.ToInt32()
可以使用任何实现IConvertible
类。 如果你传递一个字符串,那么它们是等价的,除了你为类型比较等获得额外的开销。如果你正在转换字符串,那么TryParse()
可能是更好的选择。
#8楼
看一下反射器:
int.Parse( “32”):
public static int Parse(string s)
{
return System.Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}
这是一个电话:
internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
{
byte* stackBuffer = stackalloc byte[1 * 0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
int num = 0;
StringToNumber(s, style, ref number, info, false);
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!HexNumberToInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
return num;
}
if (!NumberToInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
return num;
}
Convert.ToInt32( “32”):
public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}
正如第一个(Dave M's)的评论所说。
#9楼
Int32.parse(字符串)--->
Int32.Parse(string s)方法将数字的字符串表示形式转换为其等效的32位有符号整数。 当s是空引用时,它将抛出ArgumentNullException。 如果s不是整数值,则抛出FormatException。 当s表示小于MinValue或大于MaxValue的数字时,它将抛出OverflowException。 例如 :
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
result = Int32.Parse(s1); //1234
result = Int32.Parse(s2); //FormatException
result = Int32.Parse(s3); //ArgumentNullException
result = Int32.Parse(s4); //OverflowException
Convert.ToInt32(string) - > Convert.ToInt32(string s)方法转换32位有符号整数等效的指定字符串表示形式。 这反过来调用Int32.Parse()方法。 当s是空引用时,它将返回0而不是抛出ArgumentNullException。 如果s不是整数值,则抛出FormatException。 当s表示小于MinValue或大于MaxValue的数字时,它将抛出OverflowException。
例如:
result = Convert.ToInt32(s1); // 1234
result = Convert.ToInt32(s2); // FormatException
result = Convert.ToInt32(s3); // 0
result = Convert.ToInt32(s4); // OverflowException
#10楼
int.Parse(string s)
- RANGE中的整数>返回整数值
- 空值> ArguementNullException
- 不是格式> FormatException
- 值不在RANGE> OverflowException中
Convert.ToInt32(string s)
- RANGE中的整数>返回整数值
- 空值>返回“0”
- 不是格式> FormatException
- 值不在RANGE> OverflowException中
bool isParsed = int.TryParse(string s,out res)
- RANGE中的整数>返回整数值,isParsed = true
- 空值>返回“0”,isParsed = false
- 格式不>返回“0”,isParsed = false
- 值不在RANGE>中返回“0”,isParsed = false
请尝试以下代码......
class Program
{
static void Main(string[] args)
{
string strInt = "24532";
string strNull = null;
string strWrongFrmt = "5.87";
string strAboveRange = "98765432123456";
int res;
try
{
// int.Parse() - TEST
res = int.Parse(strInt); // res = 24532
res = int.Parse(strNull); // System.ArgumentNullException
res = int.Parse(strWrongFrmt); // System.FormatException
res = int.Parse(strAboveRange); // System.OverflowException
// Convert.ToInt32(string s) - TEST
res = Convert.ToInt32(strInt); // res = 24532
res = Convert.ToInt32(strNull); // res = 0
res = Convert.ToInt32(strWrongFrmt); // System.FormatException
res = Convert.ToInt32(strAboveRange); //System.OverflowException
// int.TryParse(string s, out res) - Test
bool isParsed;
isParsed = int.TryParse(strInt, out res); // isParsed = true, res = 24532
isParsed = int.TryParse(strNull, out res); // isParsed = false, res = 0
isParsed = int.TryParse(strWrongFrmt, out res); // isParsed = false, res = 0
isParsed = int.TryParse(strAboveRange, out res); // isParsed = false, res = 0
}
catch(Exception e)
{
Console.WriteLine("Check this.\n" + e.Message);
}
}
#11楼
这是int.Parse
和Convert.ToInt32
的详细信息:比如说,你有一个char数组, char[] a=['1','2','3','4']
并希望将每个元素转换成整数。 Convert.ToInt32(a[0])
将为您提供49个数字。它将其视为ASCII代码int.Parse(a[0])
将为您提供正确的输出,即1
如果你有一个字符串数组string[] b=['1','2','3','4']
,那么Convert.ToInt32
和int.Parse
在输出上没有区别。 两者都返回正确的整数。
#12楼
Parse()方法提供了不能用于Convert()的数字样式。 例如:
int i;
bool b = int.TryParse( "123-",
System.Globalization.NumberStyles.AllowTrailingSign,
System.Globalization.CultureInfo.InvariantCulture,
out i);
将使用尾随符号解析数字,以便i == -123
尾随标志在ERP系统中很流行。
#13楼
Convert.ToInt32允许空值,它不会抛出任何错误Int.parse不允许空值,它会抛出ArgumentNullException错误。