日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發(fā)布時間:2025/3/17 C# 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三種方法有何區(qū)別?

??? int 關(guān)鍵字表示一種整型,是32位的,它的 .NET Framework 類型為 System.Int32。

??? (int)表示使用顯式強制轉(zhuǎn)換,是一種類型轉(zhuǎn)換。當(dāng)我們從 int 類型到 long、float、double 或decimal 類型,可以使用隱式轉(zhuǎn)換,但是當(dāng)我們從 long 類型到 int? 類型轉(zhuǎn)換就需要使用顯式強制轉(zhuǎn)換,否則會產(chǎn)生編譯錯誤。

??? Int32.Parse()表示將數(shù)字的字符串轉(zhuǎn)換為32 位有符號整數(shù),屬于內(nèi)容轉(zhuǎn)換[1]。
??? 我們一種常見的方法:public static int Parse(string)。
??? 如果 string 為空,則拋出 ArgumentNullException 異常;
??? 如果 string 格式不正確,則拋出 FormatException 異常;
??? 如果 string 的值小于 MinValue 或大于 MaxValue 的數(shù)字,則拋出 OverflowException 異常。


??? Convert.ToInt32() 則可以將多種類型(包括 object? 引用類型)的值轉(zhuǎn)換為 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()三者的應(yīng)用舉幾個例子:????

??? 例子一:

??? long longType = 100;
??? int intType? = longType;?????? // 錯誤,需要使用顯式強制轉(zhuǎn)換
??? int intType = (int)longType; //正確,使用了顯式強制轉(zhuǎn)換

??? 例子二:

??? string stringType = "12345";?
??? int intType = (int)stringType;????????????????//錯誤,string 類型不能直接轉(zhuǎn)換為 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;??????????????????????????????? //雖然運行正確,但是得出錯誤結(jié)果
??? int intType = Convert.ToInt32(doubleType)??????????? //拋出 OverflowException 異常?

???(int)和Int32.Parse(),Convert.ToInt32()三者的區(qū)別:

??? 第一個在對long 類型或是浮點型到int 類型的顯式強制轉(zhuǎn)換中使用,但是如果被轉(zhuǎn)換的數(shù)值大于 Int32.MaxValue 或小于 Int32.MinValue,那么則會得到一個錯誤的結(jié)果。

??? 第二個在符合數(shù)字格式的 string 到 int? 類型轉(zhuǎn)換過程中使用,并可以對錯誤的 string 數(shù)字格式的拋出相應(yīng)的異常。

??? 第三個則可以將多種類型的值轉(zhuǎn)換為 int 類型,也可以對錯誤的數(shù)值拋出相應(yīng)的異常。

??? 無論進行什么類型的數(shù)值轉(zhuǎn)換,數(shù)值的精度問題都是我們必須考慮的[1]。

?

????本文引用:

??? [1]?http://zhidao.baidu.com/question/5393280.html

??? [2] CSDN?

?

原文:http://blog.csdn.net/scucj/article/details/846344

總結(jié)

以上是生活随笔為你收集整理的在 C# 中,(int) ,Int32.Parse() 和 Convert.toInt32() 三种方法的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。