您的位置:首页 > 其它

数据类型转换的三种方式 Convert,parse和TryParse的解析

2015-10-30 17:42 281 查看
以Int类型为例,具体说明Convert.ToInt32(object value),int.Parse(object value)和int.TryParse(string s,out int result)的用法

一.int.Parse

int.Parse的底层实现原理(可以直接忽略,不需深究)

[SecuritySafeCritical]
internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
{
byte* stackBuffer = stackalloc byte[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;
}


int.Parse()是一种类容转换;表示将数字内容的字符串转为int类型。

如果字符串为空,则抛出ArgumentNullException异常;

如果字符串内容不是数字,则抛出FormatException异常;

如果字符串内容所表示数字超出int类型可表示的范围,则抛出OverflowException异常;

二.Convert.ToInt32

Convert.ToInt32实现方式是这样的(反射源程序集可知):

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


从上面的代码可以看出Convert.toInt32其实可以看作是对 int.Parse一个改进,因为它判断了值等于null的情况

Convert.ToInt32 参数为 null 时,返回 0

但当Convert.ToInt32参数为string.empty是,就会抛出System.FormatException: 输入字符串的格式不正确异常。

三. int.TryParse

[SecuritySafeCritical]
internal static unsafe bool TryParseInt32(string s, NumberStyles style, NumberFormatInfo info, out int result)
{
byte* stackBuffer = stackalloc byte[0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
result = 0;
if (!TryStringToNumber(s, style, ref number, info, false))
{
return false;
}
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!HexNumberToInt32(ref number, ref result))
{
return false;
}
}
else if (!NumberToInt32(ref number, ref result))
{
return false;
}
return true;
}


int.TryParse 可以看作是对int.Parse和Convert.toInt32的改进。

它既判断了值等于null的情况,还判断了string.empty 这样空字符的情况。

所以它不会产生异常,转换成功返回 true,转换失败返回 false。

最后一个参数为输出值,如果转换失败,输出值为 0,如果转换成功,输出值为转换后的int值

四.int.Parse,Convert.ToInt和int.TryParse的比较

1.参数和适用对象不同

int.Parse的参数数据类型只能是string类型,适用对象为string类型的数据

convert.toInt参数比较多,具体可以参见最下面的重载列表

int.TryParse的参数只能是只能是string类型,适用对象为string类型的数据

2.异常情况不同

异常主要是针对数据为null或者为""的情况

Convert.ToInt32 参数为 null 时,返回 0;Convert.ToInt32 参数为 "" 时,抛出异常;

int.Parse 参数为 null 时,抛出异常。; int.Parse 参数为 "" 时,抛出异常。

int.TryParse

3.返回值不同

int.TryParse与int.Parse和Convert.ToInt 在返回值的不同是返回bool类型。获取转换后的值是通过out result这个参数获取的。

五.附

Convert.ToInt 参数重载列表

名称描述



ToInt32(Boolean)将指定的布尔值转换为等效的 32 位带符号整数。



ToInt32(Byte)将指定的 8 位无符号整数的值转换为等效的 32 位有符号整数。



ToInt32(Char)将指定的 Unicode 字符的值转换为等效的 32 位有符号整数。



ToInt32(DateTime)调用此方法始终引发 InvalidCastException



ToInt32(Decimal)将指定的十进制数的值转换为等效的 32 位带符号整数。



ToInt32(Double)将指定的双精度浮点数的值转换为等效的 32 位带符号整数。



ToInt32(Int16)Converts the value of the specified 16-bit signed integer to an equivalent 32-bit signed integer.



ToInt32(Int32)返回指定的 32 位有符号整数;不执行实际的转换。



ToInt32(Int64)Converts the value of the specified 64-bit signed integer to an equivalent 32-bit signed integer.



ToInt32(Object)将指定对象的值转换为 32 位带符号整数。



ToInt32(SByte)将指定的 8 位带符号整数的值转换为等效的 32 位带符号整数。



ToInt32(Single)将指定的单精度浮点数的值转换为等效的 32 位带符号整数。



ToInt32(String)将数字的指定字符串表示形式转换为等效的 32 位带符号整数。



ToInt32(UInt16)将指定的 16 位无符号整数的值转换为等效的 32 位有符号整数。



ToInt32(UInt32)将指定的 32 位无符号整数的值转换为等效的 32 位有符号整数。



ToInt32(UInt64)Converts the value of the specified 64-bit unsigned integer to an equivalent 32-bit signed integer.



ToInt32(Object, IFormatProvider)使用指定的区域性特定格式信息,将指定对象的值转换为 32 位带符号整数。



ToInt32(String, IFormatProvider)使用指定的区域性特定格式设置信息,将数字的指定字符串表示形式转换为等效的 32 位带符号整数。



ToInt32(String, Int32)将指定基数的数字的字符串表示形式转换为等效的 32 位有符号整数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: