練習 - TryParse() 方法

已完成

使用資料時,您可能需要將字串資料轉換成數值資料類型。 如同您在上一個單元中所學,因為字串資料類型可以保存非數值,所以從 string 轉換成數值資料類型可能會造成執行階段錯誤。

例如,下列程式碼:

string name = "Bob";
Console.WriteLine(int.Parse(name));

會產生下列例外狀況:

System.FormatException: 'Input string was not in a correct format.'

若要避免格式例外狀況,請在目標資料類型上使用 TryParse () 方法。

使用 TryParse()

TryParse() 方法會同時執行數項操作:

  • 此方法會嘗試將字串剖析為指定的數值資料類型。
  • 如果成功,此方法會將轉換後的值儲存在 out 參數中。
  • 它會傳回 , bool 指出動作是否成功或失敗。

我們可以使用 bool 來對值採取動作 (,例如執行某些計算) ,或在剖析作業失敗時顯示訊息。

注意

在本練習中,我們使用的是 int 資料類型,但在所有數值資料類型上都可以使用類似的 TryParse() 方法。

什麼是 out 參數?

方法可以傳回值或傳回 "void",這表示不會傳回任何值。 方法也可以透過 out 參數傳回值,這些值的定義方式就和任何其他的輸入參數相同,但包含 out 關鍵字。

呼叫使用 out 參數的方法時,您還必須在變數之前使用關鍵字 out,這會保留值。 因此,您必須先定義變數,再呼叫將用來儲存 out 參數值的方法。 接著,您可以在程式碼的其餘部分使用包含 out 參數的值。

步驟 1 - 將字串 TryParse() 至 int

在 .NET 編輯器中刪除先前練習的所有程式碼或將其標示為註解,然後新增下列程式碼:

string value = "102";
int result = 0;
if (int.TryParse(value, out result))
{
    Console.WriteLine($"Measurement: {result}");
}
else
{
    Console.WriteLine("Unable to report the measurement.");
}

讓我們重點討論這一行:

if (int.TryParse(value, out result))

如果 int.TryParse() 方法成功將 string 變數 value 轉換成 int,則會傳回 true;否則會傳回 false。 因此,請將此陳述式包圍在 if 陳述式中,並根據該陳述式執行決策邏輯。

請注意,轉換後的值會儲存在 int 變數 result 中。 在這行程式碼之前,會宣告並將 int 變數 result 初始化,因此其應該可以同時透過屬於 ifelse 陳述式的程式碼區塊內部,以及其外部存取。

out 關鍵字會指示編譯器,告知其 TryParse() 方法不只會以傳統方式傳回值 (作為傳回值),同時也會透過這個雙向參數傳達輸出。

當您執行程式碼時,應會得到下列輸出:

Measurement: 102

步驟 2 - 稍後在程式碼中使用剖析後的 int

為了示範先前宣告後由 out 參數填入的 result,稍後也可以在程式碼中使用,請在您於步驟 1 中撰寫的程式碼下方新增下列這行程式碼:

// Since it is defined outside of the if statement, 
// it can be accessed later in your code.
Console.WriteLine($"Measurement (w/ offset): {50 + result}");

整個程式碼片段應該符合下列程式碼清單:

string value = "102";
int result = 0;
if (int.TryParse(value, out result))
{
    Console.WriteLine($"Measurement: {result}");
}
else
{
    Console.WriteLine("Unable to report the measurement.");
}

// Since it is defined outside of the if statement, 
// it can be accessed later in your code.
Console.WriteLine($"Measurement (w/ offset): {50 + result}");

當您執行應用程式時,應該會看見下列結果:

Measurement: 102
Measurement (w/ offset): 152

步驟 3 - 將字串變數修改為無法剖析的值

最後,讓我們看一下另一個案例,在此案例中,我們故意提供 TryParse() 一個不能轉換成 int 的錯誤值。

修改程式碼的第一行,將變數 value 重新初始化為不同的值:

string value = "bad";

此外,修改最後一行程式碼,以確保結果在顯示第二則訊息之前大於零:

if (result > 0)
    Console.WriteLine($"Measurement (w/ offset): {50 + result}");

整個程式碼範例應該符合下列程式碼:

string value = "bad";
int result = 0;
if (int.TryParse(value, out result))
{
    Console.WriteLine($"Measurement: {result}");
}
else
{
    Console.WriteLine("Unable to report the measurement.");
}

// Since it is defined outside of the if statement, 
// it can be accessed later in your code.
if (result > 0)
    Console.WriteLine($"Measurement (w/ offset): {50 + result}");

當您執行程式碼指派時,應該會取得下列結果:

Unable to report the measurement.

概括回顧

TryParse() 方法是很重要的工具。 以下是一些要記住的快速概念:

  • 將字串轉換成數值資料類型時,請使用 TryParse()
  • 如果轉換成功,TryParse() 會傳回 true;如果不成功,則傳回 false
  • out 參數提供傳回值之方法的次要方式。 在此案例中,out 參數會傳回轉換後的值。
  • 將引數傳入已定義 out 參數的方法時,請使用關鍵字 out