C#强制转换:(int)、Int32.Parse() 和 Convert.toInt32()

开发 后端
本文介绍了C#强制转换问题,即(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]:

 

  1. public static int ToInt32(object);  
  2.  
  3.  public static int ToInt32(bool);  
  4.  
  5.  public static int ToInt32(byte);  
  6.  
  7.  public static int ToInt32(char);  
  8.  
  9.  public static int ToInt32(decimal);  
  10.  
  11.  public static int ToInt32(double);  
  12.  
  13.  public static int ToInt32(short);  
  14.  
  15.  public static int ToInt32(long);  
  16.  
  17.  public static int ToInt32(sbyte);  
  18.  
  19.  public static int ToInt32(string);  
  20.  
  21.  ......  

(int)和Int32.Parse(),Convert.ToInt32()三者的应用举几个例子:   

例子一:

  1. long longType = 100;  
  2. int intType  = longType;       // 错误,需要使用显式强制转换  
  3. int intType = (int)longType; //正确,使用了显式强制转换 

例子二:

  1. string stringType = "12345";   
  2. int intType = (int)stringType;                //错误,string 类型不能直接转换为 int  类型   
  3. int intType = Int32.Parse(stringType);   //正确 

例子三:

  1. long longType = 100;  
  2. string stringType = "12345";  
  3. object objectType = "54321";  
  4. int intType = Convert.ToInt32(longType);       //正确  
  5. int intType = Convert.ToInt32(stringType);     //正确  
  6. int intType = Convert.ToInt32(objectType);    //正确 

例子四[1]:

  1. double doubleType = Int32.MaxValue + 1.011;   
  2. int intType = (int)doubleType;                                //虽然运行正确,但是得出错误结果  
  3. int intType = Convert.ToInt32(doubleType)            //抛出 OverflowException 异常  

C#强制转换中(int)和Int32.Parse(),Convert.ToInt32()三者的区别:

***个在对long 类型或是浮点型到int 类型的显式强制转换中使用,但是如果被转换的数值大于Int32.MaxValue 或小于 Int32.MinValue,那么则会得到一个错误的结果。

第二个在符合数字格式的string到int 类型转换过程中使用,并可以对错误的string数字格式的抛出相应的异常。

第三个则可以将多种类型的值转换为int类型,也可以对错误的数值抛出相应的异常。

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

以上就是C#强制转换中的相关问题,供大家参考。

【编辑推荐】

  1. C#调用VC DLL接口函数参数类型转换方法介绍
  2. 解决C#中用Oracle执行存储过程返回DataSet的问题
  3. C#线程同步技术之Monitor
  4. C#线程同步与死锁
  5. C#线程:线程池和文件下载服务器
责任编辑:book05 来源: cnblogs
相关推荐

2009-08-21 17:07:53

C# Convert.

2009-08-26 15:04:35

C#转换

2009-08-13 17:25:18

C# Convert类

2023-09-14 16:02:27

2011-06-17 16:42:23

C#

2009-09-01 17:06:20

C#命名管道

2009-08-24 14:20:13

C# 强制类型转换

2024-03-19 14:18:48

C#后端编程

2009-08-28 15:45:32

C#操作Win32 A

2009-07-31 15:47:20

Win32 APIC#

2021-08-30 07:22:15

Go类型interface

2021-07-27 06:49:11

数据库设计数据

2009-09-01 16:35:55

C#操作String数

2023-12-12 08:50:22

MySQL隐式转换varchar

2009-08-21 09:57:08

C#操作符IS与AS

2023-01-17 14:01:19

JavaScript类型转换字符串

2021-08-31 09:12:18

StringIntLong

2021-03-08 15:33:46

Windows10操作系统微软

2009-06-05 11:16:58

字符串动态转换

2009-08-12 18:28:49

C#强制类型转化
点赞
收藏

51CTO技术栈公众号