Home [C# 筆記] 型別.Parse 方法
Post
Cancel

[C# 筆記] 型別.Parse 方法

資料型別的轉換方法

資料型別的三種轉換方法:

  • System.Convert轉換
  • 型別.Parse方法
  • 指定轉換(Cast)

由於C# 是強型別(Strongly-Typed)而非弱型別(Weakly-Typed),所以在執行時期(Runtime)資料型別通常需要明確地宣告才能通過編譯器的嚴苛考驗。

型別.Parse 方法

Parse()方法主要將「指定字串」的內容轉換成指定的型別資料。

這個意思就是表示Parse()方法主要處理對象是「字串(String)」。

1
2
string s = "1999-09-09";
DateTime dt = DateTime.Parse(s);

常犯的錯誤

在使用Parse()方法最常犯的錯誤就是 「轉換的內容不是字串(String)」

以下錯誤範例:

1
2
float score = 99.0f;
double newScore = double.Parse(score); //報錯

上面的錯誤為:score 是float型態並不是string型態,所以進行double.Parse(score)方法時就會無法進行編譯而產生「引數無效」的錯誤。

將程式改成下面,就能順利執行編譯:

1
2
float score = 99.0f;
double newScore = double.Parse(score.ToString()); //score改成string型態

int.Parse() 對 null 的處理

int.Parse(null)會產生異常、產生Exception
Convert.ToInt32(null)會返回0不會產生任何異常。

1
2
int.Parse(null); //會產生異常、產生Exception
Convert.ToInt32(null); //會返回0,不會產生任何異常。

int.Parse() 對小數點的處理

int.Parse 有小數點會報錯,發生異常。

結論

  • int.Parse()只能轉換 string 類型。
  • int.Parse(null)會產生異常、產生Exception
  • 有小數就不能用int.Parse(),會報錯。例如:int.Parse("4.5"); //報錯

[C# 筆記](int)、Convert.ToInt32、int.Parse、int.TryParse by R
Book: Visual C# 2005 建構資訊系統實戰經典教本

This post is licensed under CC BY 4.0 by the author.