C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别
来源:互联网 发布:8051单片机引脚映射 编辑:程序博客网 时间:2024/04/29 17:44
在 C# 中,(int),Int32.Parse()和 Convert.toInt32() 三种方法有何区别?
int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 System.Int32。
(int)表示使用显式强制转换,是一种类型转换。当我们从 int 类型到 long、float、double 或decimal 类型,可以使用隐式转换,但是当我们从 long 类型到 int 类型转换就需要使用显式强制转换,否则会产生编译错误。
Int32.Parse()表示将数字的字符串转换为32 位有符号整数,属于内容转换[1]。
我们一种常见的方法:publicstatic int Parse(string)。
如果 string 为空,则抛出 ArgumentNullException 异常;
如果 string 格式不正确,则抛出 FormatException 异常;
如果 string 的值小于 MinValue 或大于 MaxValue 的数字,则抛出 OverflowException 异常。
Convert.ToInt32() 则可以将多种类型(包括 object 引用类型)的值转换为 int 类型,因为它有许多重载版本[2]:
public staticint ToInt32(object);
public staticint ToInt32(bool);
public staticint ToInt32(byte);
public staticint ToInt32(char);
public staticint ToInt32(decimal);
public staticint ToInt32(double);
public staticint ToInt32(short);
public staticint ToInt32(long);
public staticint ToInt32(sbyte);
public staticint ToInt32(string);
......
(int)和Int32.Parse(),Convert.ToInt32()三者的应用举几个例子:
例子一:
long longType =100;
int intType = longType; // 错误,需要使用显式强制转换
int intType =(int)longType; //正确,使用了显式强制转换
例子二:
stringstringType = "12345";
int intType =(int)stringType; //错误,string 类型不能直接转换为 int 类型
int intType =Int32.Parse(stringType); //正确
例子三:
long longType =100;
stringstringType = "12345";
objectobjectType = "54321";
int intType =Convert.ToInt32(longType); //正确
int intType =Convert.ToInt32(stringType); //正确
int intType =Convert.ToInt32(objectType); //正确
例子四[1]:
doubledoubleType = 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 类型,也可以对错误的数值抛出相应的异常。
无论进行什么类型的数值转换,数值的精度问题都是我们必须考虑的
1.任何一门编程语言均有相关数据类型。C#也不例外,其基本数据类型有int,short,long,float,double,string等。数据类型之间可以相互转换。不过转换过程要注意小类型能转换成大类型,但大类型一般不能转换成小类型。如int型可以转换成float型,但float型不一定可以转换成int型,至少这在C,C++是这样,但在C#中明显有了改变,似乎微软公司也允许这样的形式存在了。例如:
double dbl_num=12345678910.456;
int k = (int) dbl_num ;//此处运用了强制转换
以上代码如果在C,C++中强制转换成int型,肯定会出错,但现在在C#中却不会出错了,不过转换后的值往往是溢出值,是不精通的。这点需要大家注意。
2.采用另一种方式转换类型,如int.parse(),int32.parse()等采用方法来转换.
如string str=”100″;
int i=int.Parse(str);
注意:str除掉引号的类型必须和*.Parse的类型一致。如果将100改成100.78,即变成float类型,运行时将会报错”输入字符串的格式不正确.”
3.采用后缀式转换,如k.toString(),一般运用于字符串或日期等其它类型
int i=100;
string s=i.ToString();
4.采用Convert类来实现转换,该类基本支持所以类型之间的转换
string str=”100″;
int i =Convert.ToInt16(str);
注意:str除掉引号的类型必须和Convert.*的类型一致。如果将100改成100.78,即变成float类型,运行时将会报错”输入字符串的格式不正确.”
- 在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别?
- C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别
- C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别
- C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
- 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
- (int)和Int32.Parse(),Convert.ToInt32()三者的区别
- C#中的(int)、int32.Parse() 和 Convert.ToInt32()区别
- C# 中(int),Int32.Parse() 和Convert.toInt32用法[转载]
- [C#]Int32.TryParse()、int.Parse和Convert.ToInt32的区别
- C#中Convert.ToInt32()和int.Parse()的区别
- Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别
- Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别
- Int32.Parse()和Convert.ToInt32()
- [51nod 1222]最小公倍数计数
- multimap容器案例_按照部门_增删改查员工信息
- 对于长期学习方式的一点看法
- C# 通过URL获取图片并显示在PictureBox上的方法
- “error LNK2019: 无法解析的外部符号”的几种可能原因
- C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别
- Spark SQL saveMode 方式
- leetcode oj java 230. Kth Smallest Element in a BST
- 不带缓冲的文件I/O操作
- Tomcat启动失败-提示Server Tomcat v7.0 Server at localhost failed to start.
- 重构项目中犯的一些低级错误
- cocos-Lua table积累
- python 遍历文件目录并修改包含某字符串的文件名字
- PHP处理excel文件 包括处理excel数据上传到数据库以及从数据库下载数据到excel