Skip to content

ZH TW CSharp Conditions

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

條件及切換

手冊C# · 範例UO

if接受bool。根據合約,透過明確比較來查看比賽隊伍的數值結果,例如result != 0;如果沒有命令說明,請勿將任何零視為錯誤。

簡單範例 (Example.cs)

請將此範例另存為 Example.cs,並與本頁其他範例分開執行。

Example.cs:

// PROJECT-EXAMPLE
int health = 80;
if (health < 50) System.Console.WriteLine("Low");
else System.Console.WriteLine("Ready");

結果:

Ready
int hp = UO.GetHP();
if (hp > 50)
{
    UO.Print("High HP");
}
else if (hp > 0)
{
    UO.Print("HP is available");
}
else
{
    UO.Print("Check character state");
}

switch運算子選擇具有case、when和default的分支。 switch-表達式傳回一個值。分支可以使用類型、屬性和範圍模板。

int distance = 3;
string label = distance switch
{
    0 => "here",
    <= 2 => "near",
    < 10 => "reachable area",
    _ => "far"
};
UO.Print(label);

官方文件:條件與切換 - Microsoft Learn。

Clone this wiki locally