以Int類型為例,具體說明Convert.ToInt32(object value),int.Parse(object value)和int.TryParse(string s,out int result)的用法
一.int.Parse
int.Parse的底層實現原理(可以直接忽略,不需深究)
[SecuritySafeCritical] internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info) { byte* stackBuffer = stackalloc byte[0x72]; NumberBuffer number = new NumberBuffer(stackBuffer); int num = 0; StringToNumber(s, style, ref number, info, false); if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None) { if (!HexNumberToInt32(ref number, ref num)) { throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); } return num; } if (!NumberToInt32(ref number, ref num)) { throw new OverflowException(Environment.GetResourceString("Overflow_Int32")); } return num; }
int.Parse()是一種類容轉換;表示將數字內容的字符串轉為int類型。
如果字符串為空,則拋出ArgumentNullException異常;
如果字符串內容不是數字,則拋出FormatException異常;
如果字符串內容所表示數字超出int類型可表示的范圍,則拋出OverflowException異常;
二.Convert.ToInt32
Convert.ToInt32實現方式是這樣的(反射源程序集可知):
public static int ToInt32(string value) { if (value == null) { return 0; } return int.Parse(value, CultureInfo.CurrentCulture); }
從上面的代碼可以看出Convert.toInt32其實可以看作是對 int.Parse一個改進,因為它判斷了值等於null的情況
Convert.ToInt32 參數為 null 時,返回 0
但當Convert.ToInt32參數為string.empty是,就會拋出System.FormatException: 輸入字符串的格式不正確異常。
三. int.TryParse
[SecuritySafeCritical] internal static unsafe bool TryParseInt32(string s, NumberStyles style, NumberFormatInfo info, out int result) { byte* stackBuffer = stackalloc byte[0x72]; NumberBuffer number = new NumberBuffer(stackBuffer); result = 0; if (!TryStringToNumber(s, style, ref number, info, false)) { return false; } if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None) { if (!HexNumberToInt32(ref number, ref result)) { return false; } } else if (!NumberToInt32(ref number, ref result)) { return false; } return true; }
int.TryParse 可以看作是對int.Parse和Convert.toInt32的改進。
它既判斷了值等於null的情況,還判斷了string.empty 這樣空字符的情況。
所以它不會產生異常,轉換成功返回 true,轉換失敗返回 false。
最后一個參數為輸出值,如果轉換失敗,輸出值為 0,如果轉換成功,輸出值為轉換后的int值
四.int.Parse,Convert.ToInt和int.TryParse的比較
1.參數和適用對象不同
int.Parse的參數數據類型只能是string類型,適用對象為string類型的數據
convert.toInt參數比較多,具體可以參見最下面的重載列表
int.TryParse的參數只能是只能是string類型,適用對象為string類型的數據
2.異常情況不同
異常主要是針對數據為null或者為""的情況
Convert.ToInt32 參數為 null 時,返回 0;Convert.ToInt32 參數為 "" 時,拋出異常;
int.Parse 參數為 null 時,拋出異常。; int.Parse 參數為 "" 時,拋出異常。
int.TryParse
3.返回值不同
int.TryParse與int.Parse和Convert.ToInt 在返回值的不同是返回bool類型。獲取轉換后的值是通過out result這個參數獲取的。
五.附
Convert.ToInt 參數重載列表
| 名稱 | 描述 | |
|---|---|---|
![]() |
ToInt32(Boolean) | 將指定的布爾值轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(Byte) | 將指定的 8 位無符號整數的值轉換為等效的 32 位有符號整數。 |
![]() |
ToInt32(Char) | 將指定的 Unicode 字符的值轉換為等效的 32 位有符號整數。 |
![]() |
ToInt32(DateTime) | 調用此方法始終引發 InvalidCastException。 |
![]() |
ToInt32(Decimal) | 將指定的十進制數的值轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(Double) | 將指定的雙精度浮點數的值轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(Int16) | Converts the value of the specified 16-bit signed integer to an equivalent 32-bit signed integer. |
![]() |
ToInt32(Int32) | 返回指定的 32 位有符號整數;不執行實際的轉換。 |
![]() |
ToInt32(Int64) | Converts the value of the specified 64-bit signed integer to an equivalent 32-bit signed integer. |
![]() |
ToInt32(Object) | 將指定對象的值轉換為 32 位帶符號整數。 |
![]() |
ToInt32(SByte) | 將指定的 8 位帶符號整數的值轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(Single) | 將指定的單精度浮點數的值轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(String) | 將數字的指定字符串表示形式轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(UInt16) | 將指定的 16 位無符號整數的值轉換為等效的 32 位有符號整數。 |
![]() |
ToInt32(UInt32) | 將指定的 32 位無符號整數的值轉換為等效的 32 位有符號整數。 |
![]() |
ToInt32(UInt64) | Converts the value of the specified 64-bit unsigned integer to an equivalent 32-bit signed integer. |
![]() |
ToInt32(Object, IFormatProvider) | 使用指定的區域性特定格式信息,將指定對象的值轉換為 32 位帶符號整數。 |
![]() |
ToInt32(String, IFormatProvider) | 使用指定的區域性特定格式設置信息,將數字的指定字符串表示形式轉換為等效的 32 位帶符號整數。 |
![]() |
ToInt32(String, Int32) | 將指定基數的數字的字符串表示形式轉換為等效的 32 位有符號整數。 |
