在 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 类型,也可以对错误的数值抛出相应的异常。

无论进行什么类型的数值转换,数值的精度问题都是我们必须考虑的。

(int),Int32.Parse() 和 Convert.toInt32() 的区别的更多相关文章

  1. .net中三种数据类型转换区别((int),Int32.Parse() 和 Convert.toInt32() )

    (typename)valuename,是通用方法: Convert类提供了灵活的类型转换封装: Parse方法,适用于向数字类型的转换. 例如,(int),Int32.Parse() 和 Conve ...

  2. 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别

    在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别? int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 S ...

  3. 【c#文档】在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别

    [c#文档]https://msdn.microsoft.com/zh-cn/library/system.convert.toint32.aspx 转载自:http://www.cnblogs.co ...

  4. Int.Parse()、Convert.toInt32()和(int)区别

    通过网上的查询从而了解了Int.Parse().Convert.toInt32()和(int)区别. 一.定义上的差别 int类型表示一种整型,.NET Framework 类型为 System.In ...

  5. (int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    C#中(int).int.Parse().int.TryParse()和Convert.ToInt32()的区别   原文链接:http://www.cnblogs.com/leolis/p/3968 ...

  6. int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    1:int.Parse(一个参数)        此参数必须满足: 1 只能是字符串: 2 只能是 “整型” 字符串,即各种整型ToString()之后的形式,也不能为浮点型. 2:int.TryPa ...

  7. C# int.Parse()、int.TryParse()与Convert.ToInt32()的区别

    1.(int)是一种类型转换:当我们觟nt类型到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需要使用显式转换,否则会产生编译错误. ...

  8. int.Tryparse() 、int.parse()、Convert.To32() 的区别

    int.Tryparse()  Int32.TryParse(source, result)则无论如何都不抛出异常,只会返回true或false来说明解析是否成功,如果解析失败,调用方将会得到0值. ...

  9. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...

随机推荐

  1. beta分工

    队伍CleanCode 031302505   黄晓辉(21%) 031302223   翁瀚帅(19%) 031302511   林培兴(22%) 031302632   张衍坤(19%) 0313 ...

  2. java定时器的使用(Timer)

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Tim ...

  3. Canvas识别相似图片

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. SQL Server之存储过程基础知识

    什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...

  5. load and initialize

    NSObject是一切OC类的基类,所以我们必须对NSObject所有的方法有一个清楚的认识. + (void)load; 当类或者分类被加入到runtime时,load方法会被调用,也就是说在mai ...

  6. 洛谷1352 CODEVS1380 没有上司的舞会

    洛谷的测试数据貌似有问题,4个点RE不可避 CODEVS可AC —————— 10分钟后追记:在洛谷把数组范围开到10000+就过了 —————— 题目描述 Description Ural大学有N个 ...

  7. c++标准库和stl关系

    C++标准库的所有头文件都没有扩展名.C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能. <cname>形式的标准头文件[ <complex>例外]其 ...

  8. python 与 mysql

    1.开发环境: 1)CLion-2016.1.3 C/C++ 与 Python 混合编程 IDE,先安装好以下 2) 3) 编译器再关联 2)tdm-gcc-4.8.1-3 C/C++ 编译器 3)W ...

  9. C语言计算任意数的任意次方

    #include "stdio.h" #include"stdlib.h" #define max 500 void yiwei(int *a,int n,in ...

  10. VirtualBox下安装rhel5.5 linux系统

    以前也用过VMware server和VMware workstation虚拟机,现在使用了一段时间VirtualBox,感觉它比较轻巧,很适合我,在Win7系统下用起来很方便.下面详细介绍下在Vir ...