【c#文档】在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别
【c#文档】https://msdn.microsoft.com/zh-cn/library/system.convert.toint32.aspx
转载自:http://www.cnblogs.com/wengjinbao/articles/696716.html
在 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 类型,也可以对错误的数值抛出相应的异常。
无论进行什么类型的数值转换,数值的精度问题都是我们必须考虑的
【c#文档】在 C# 中,(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.toInt32() 的区别
在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别? int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 S ...
- .net中三种数据类型转换区别((int),Int32.Parse() 和 Convert.toInt32() )
(typename)valuename,是通用方法: Convert类提供了灵活的类型转换封装: Parse方法,适用于向数字类型的转换. 例如,(int),Int32.Parse() 和 Conve ...
- jQuery中detach&&remove&&empty三种方法的区别
jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...
- VC中加载LIB库文件的三种方法
VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...
- Linux 中执行Shell 脚本的方式(三种方法)
Shell 脚本的执行方式通常有如下三种: (1)bash script-name 或者 sh script-name:(2)path/script-name或者./script-name:(3)so ...
- linux中使用head,tail,grep, sed,awk三种方法显示文档中间若干行(指定任意行)
需要显示文本中间20-25行. 创建一个30行的文档,命名为30.txt并显示在屏幕 [root@v2-ui data]# seq 30 > 30.txt && cat 30.t ...
- Java中实现十进制数转换为二进制的三种方法
第一种:除基倒取余法 这是最符合我们平时的数学逻辑思维的,即输入一个十进制数n,每次用n除以2,把余数记下来,再用商去除以2...依次循环,直到商为0结束,把余数倒着依次排列,就构成了转换后的二进制数 ...
- Java中创建线程的三种方法以及区别
Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例.Java可以用三种方式来创建线程,如下所示: 1)继承Thread类创建线程 2)实现Runnable接口创建线 ...
随机推荐
- ArcGIS 工作经历【IFeatureBuffer】【CAD转SHP】
由于工作需要,需要基于ArcGIS进行二次开发,软件的开发过程当中有一个非常重要的功能,就是需要把CAD的数据转换为shp文件保存,方便后面的使用编辑,存储. 先说一下功能的前提,需要将CAD转换为s ...
- Qt绘制简单的风向玫瑰图代码
1.绘制简单的风向玫瑰图代码2.主要使用QPainter3.在子widget上绘制需要使用widget监视事件 eventfilter update();//更新界面 //镜头12 QPainter ...
- .NET Framework的一些基本概念
各种Framework的区别(按在Windows程序管理中显示的名称) .NET Framework: 运行环境,仅用于运行程序 .NET Framework Developer Pack: 包含Ru ...
- NSRange 范围
前言 结构体,这个结构体用来表示事物的一个范围,通常是字符串里的字符范围或者集合里的元素范围. typedef struct _NSRange { NSUInteger location; // 表示 ...
- requests库和urllib包对比
python中有多种库可以用来处理http请求,比如python的原生库:urllib包.requests类库.urllib和urllib2是相互独立的模块,python3.0以上把urllib和ur ...
- DIV做的Table
<style> div.table{ border:1px solid #d7d7d7; margin-left:0px; border-bottom-width:; width:1200 ...
- Centos文章列表
1.Linux 中将用户添加到组的指令:https://cnzhx.net/blog/linux-add-user-to-group/ 2.CentOS7为firewalld添加开放端口及相关操作:h ...
- gitlab忘记管理员密码
gitlab忘记密码后破解! gitlab-rails console production :> user = User.).first irb(main)::> user.passwo ...
- Little Elephant and Magic Square
Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains s ...
- #537 (Div. 2) Creative Snap (思维+dfs)
https://codeforces.com/contest/1111/problem/C 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出).你是灭霸你要用以下方法消灭这 ...