.net 基礎知識大雜燴(2) ——條件陳述式

來源:互聯網
上載者:User
If 語句

C# VB
int i = 3;
if (i < 5)
{
    Console.WriteLine("i 小於 5");
}
Dim i As Integer = 3
If i < 5 Then
    Console.WriteLine("i 小於 5")
End If
int i = 9;
if (!(i < 5))
{
    Console.WriteLine("i 大於等於 5");
}
Dim i As Integer = 9
If Not i < 5 Then
    Console.WriteLine("i 大於等於 5")
End If
int i = 3;
if (i < 5)
{
    Console.WriteLine("i 小於 5");
}
else
{
    Console.WriteLine("i 大於等於 5");
}
Dim i As Integer = 3
If i < 5 Then
    Console.WriteLine("i 小於 5")
Else
    Console.WriteLine("i 大於等於 5")
End If
int i = 3;
if (i < 5)
{
    Console.WriteLine("i 小於 5");
}
else if (i == 5)
{
    Console.WriteLine("i 等於 5");
}
else
{
    Console.WriteLine("i 大於 5");
}
Dim i As Integer = 3
If i < 5 Then
    Console.WriteLine("i 小於 5")
ElseIf i = 5 Then
    Console.WriteLine("i 等於 5")
Else
    Console.WriteLine("i 大於 5");
End If

switch 語句

C# VB
int i = 9;
switch (i)
{
    case 3:
        Console.WriteLine("i = 3");
        break;
    case 5:
        Console.WriteLine("i = 5");
        break;
    case 9:
        Console.WriteLine("i = 9");
        break;
    default:
        Console.WriteLine("other");
        break;
}
Dim i As Integer = 9
Select Case i
    Case 3
        Console.WriteLine("i = 3")
    Case 5
        Console.WriteLine("i = 5")
    Case 9
        Console.WriteLine("i = 9")
    Case Else
        Console.WriteLine("other")
End Select
int i = 5;
switch (i)
{
    case 3:
    case 5:
    case 9:
        Console.WriteLine("i = 3 or 5 or 9");
        break;
    default:
        Console.WriteLine("other");
        break;
}
Dim i As Integer = 5
Select Case i
    Case 3, 5, 9
        Console.WriteLine("i = 3 or 5 or 9")
    Case Else
        Console.WriteLine("other")
End Select
不支援 Dim i As Integer = 9
Select Case i
    Case 5 To 9
        Console.WriteLine("i = 5 to 9")
End Select
enum Tricolor { Red, Green, Blue }
// ...
Tricolor color = Tricolor.Blue;
switch (color)
{
    case Tricolor.Blue:
        Console.WriteLine("Blue");
        break;
    case Tricolor.Green:
        Console.WriteLine("Green");
        break;
    default:
        Console.WriteLine("other color");
        break;
}
Enum Tricolor
    Red
    Green
    Blue
End Enum
' ...
Dim color As Tricolor = Tricolor.Blue
Select Case color
    Case Tricolor.Blue
        Console.WriteLine("Blue")
    Case Tricolor.Green
        Console.WriteLine("Green")
    Case Else
        Console.WriteLine("other color")
End Select

技巧

1. Imposible Is Nothing

記得我在偵錯工具的時候說得最多的一句話就是“不可能啊……”。因為我們在編程的時候總是要假設很多條件的。我們會假設天不會塔下來、出門不會被車撞到、吃飯不會被噎死,否則就沒法生活下去了。例如我們會這樣寫程式:if (sex == 1)
{
    Console.WriteLine("男人");
}
else if (sex == 0)
{
    Console.WriteLine("女人");
}

因為我們心想 sex 變數不可能出現別的值。
不過一旦寫了這樣的代碼,不久之後就一定會後悔的,因為就像溫伯格所說的,“所有不可能發生的事情都一定會發生”,所以正確的寫法應該是:if (sex == 1)
{
    Console.WriteLine("男人");
}
else if (sex == 0)
{
    Console.WriteLine("女人");
}
else
{
    System.Diagnostics.Debug.Assert(false, "Should never reach here!");
}

或者這樣寫也不錯:System.Diagnostics.Debug.Assert((sex == 1 || sex == 0), "wrong sex");
if (sex == 1)
{
    Console.WriteLine("男人");
}
else
{
    Console.WriteLine("女人");
}

總之只要你寫if或switch語句,就要確保它覆蓋了變數的所有取值範圍。

2.  避免多層嵌套的if語句

相信大家都受過一層套著一層又套著一層的代碼的折磨,特別是正巧每層代碼又很長的時候,那感覺就像在熱帶雨林裡迷了路,轉過好幾圈卻不知道自己身在何處。比較下面兩段功能相同的代碼。
可讀性不好的代碼:public string foo(int age)
{
    if (age >= 0 && age <= 150)
    {
        if (age >= 0 && age <= 18)
        {
            return "青少年";
        }
        else if (age > 18 && age <= 60)
        {
            return "中年";
        }
        else
        {
            return "老年";
        }
    }
    else
    {
        return "無效的年齡";
    }
}

可讀性好的代碼:public string foo(int age)
{
    if (age<0 || age > 150)
    {
        return "無效的年齡";
    }

    if (age >= 0 && age <= 18)
    {
        return "青少年";
    }
    else if (age > 18 && age <= 60)
    {
        return "中年";
    }
    else
    {
        return "老年";
    }
}

是不是感覺一下子清新了好多?而且更重要的是,第二段代碼把對正常情況的處理和非正常情況下的處理明顯地分成了兩部分,使讀者更容易集中精力。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.