(int),Int32.Parse() 和 Convert.toInt32() 的区别
在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别?
int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 System.Int32。
(int)表示使用显式强制转换,是一种类型转换。当我们从 int 类型到 long、float、double 或decimal 类型,可以使用隐式转换,但是当我们从 long 类型到 int 类型转换就需要使用显式强制转换,否则会产生编译错误。
Int32.Parse()表示将数字的字符串转换为32 位有符号整数,属于内容转换[1]。
我们一种常见的方法:public static int Parse(string)。
如果 string 为空,则抛出 ArgumentNullException 异常;
如果 string 格式不正确,则抛出 FormatException 异常;
如果 string 的值小于 MinValue 或大于 MaxValue 的数字,则抛出 OverflowException 异常。
Convert.ToInt32() 则可以将多种类型(包括 object 引用类型)的值转换为 int 类型,因为它有许多重载版本[2]:
public static int ToInt32(object);
public static int ToInt32(bool);
public static int ToInt32(byte);
public static int ToInt32(char);
public static int ToInt32(decimal);
public static int ToInt32(double);
public static int ToInt32(short);
public static int ToInt32(long);
public static int ToInt32(sbyte);
public static int ToInt32(string);
......
(int)和Int32.Parse(),Convert.ToInt32()三者的应用举几个例子:
例子一:
long longType = 100;
int intType = longType; // 错误,需要使用显式强制转换
int intType = (int)longType; //正确,使用了显式强制转换
例子二:
string stringType = "12345";
int intType = (int)stringType; //错误,string 类型不能直接转换为 int 类型
int intType = Int32.Parse(stringType); //正确
例子三:
long longType = 100;
string stringType = "12345";
object objectType = "54321";
int intType = Convert.ToInt32(longType); //正确
int intType = Convert.ToInt32(stringType); //正确
int intType = Convert.ToInt32(objectType); //正确
例子四[1]:
double doubleType = Int32.MaxValue + 1.011;
int intType = (int)doubleType; //虽然运行正确,但是得出错误结果
int intType = Convert.ToInt32(doubleType) //抛出 OverflowException 异常
(int)和Int32.Parse(),Convert.ToInt32()三者的区别:
第一个在对long 类型或是浮点型到int 类型的显式强制转换中使用,但是如果被转换的数值大于 Int32.MaxValue 或小于 Int32.MinValue,那么则会得到一个错误的结果。
第二个在符合数字格式的 string 到 int 类型转换过程中使用,并可以对错误的 string 数字格式的抛出相应的异常。
第三个则可以将多种类型的值转换为 int 类型,也可以对错误的数值抛出相应的异常。
无论进行什么类型的数值转换,数值的精度问题都是我们必须考虑的。
转载于:https://www.cnblogs.com/eric-qin/p/4065450.html
(int),Int32.Parse() 和 Convert.toInt32() 的区别相关推荐
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别? int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 S ...
- (int),Int32.Parse,Convert.ToInt3…
(int)是一种被称为强制转换的显示转换.源变量和目标变量必须是兼容的(必须都是int类型的).并且有丢失数据的风险.因为目标变量的类型大小小于源变量. 从int到long.float.double或 ...
- (.Net常识)(int),Int32.Parse,Conver.ToInt32三者在什么情况下使用以及其区别。
三者分别侧重在以下情况下使用: (int) : 适合简单数据类型之间的转换,在对long 类型或是浮点型到int 类型的显式强制转换中使用,但是如果被转换的数值大于 Int32.MaxValue 或小 ...
- int.Parse()和Convert.ToInt32之间的主要区别是什么
int.Parse()和Convert.ToInt32()之间的主要区别是什么? 哪一个是首选 #1楼 Convert.ToInt32 有19种重载或19种不同的方式可以称之为. 2010版可能更多. ...
- 关于Convert.ToInt32和int.Parse用法总结
Convert是一个类,继承自system.Object:int是值类型 Convert.ToInt32()内部就是调用了int.Parse()方法; Convert.ToInt32()方法可以将多种 ...
- c#中什么情况下用(int)什么情况下用Convert.ToInt32
1.c#中什么情况下用(int)什么情况下用Convert.ToInt32 ? 比如说有一个string型的3 ,要给它转换成int型的是用(int)3 ,还是用Convert.ToInt32(3 ...
- C#Convert.ToInt32(byte)方法-将字节值转换为int
C#Convert.ToInt32(byte)方法 (C# Convert.ToInt32(byte) Method) Convert.ToInt32(byte) Method is used to ...
- C#Convert.ToInt32(bool)方法-将bool值转换为int
C#Convert.ToInt32(bool)方法 (C# Convert.ToInt32(bool) Method) Convert.ToInt32(bool) Method is used to ...
- 有关int,Int32的疑惑解答
疑惑1:int.System.Int32和int32的区别 1. int32是IL中的基元类型(primary type),int和System.Int32是对int32进行了封装. 2. int和S ...
最新文章
- 计算机语言编程能力有哪些,除了编程语言,程序员还需要具备哪些能力
- C#中提供的精准测试程序运行时间的类Stopwatch
- 无线节能信标调试说明-2021-3-3
- ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用
- h5微信本地调试 vue_vueh5中使用微信sdk
- javascript 内置对象学习 笔记:
- wordpress函数技巧
- WindowsXP 下搭建PHP环境(笔记)
- python 读取配置文件的单元测试_单元测试
- mysql怎么求平均体重_怎样计算平均身高和平均体重
- Struts2校验器(二)之注解
- 中兴B860AV2.1刷Armbian折腾记录
- VeraCrypt磁盘加密软件
- TCP/IP协议卷学习---基础部分(十六 BOOTP)
- 通达信版弘历软件指标_通达信仿弘历收费指标
- Maven的生命周期和插件
- 【openjudge】金银岛
- 如何 SolidWorks 减小文件大小?
- 快出狱的王欣,快播错过的三年
- oracle rac启停数据库,RAC启停全过程