2009年4月30日星期四

.NET的AndAlso與OrElse

今天看看一個比較Fresh的同事做的VB Project,看到一些(IF中IF),我問他為什麼不用AndAlso,他似乎不知道是什麼,但他寫的C# Code卻有"&&",最後我告訴他,其實"&&"是AndAlso,不是And,他就:( 哦~~~~!!!)
其實除了AndAlso之外,還有OrElse,自.NET第一代已經有。不過的確我看到很多討論區,一些學生問VB功課,他們都是用And/或者Or,可能教他們的老師都是受VB6影響。一些比較老的Code Sample或Soruce Code都比較難見到AndAlso/OrElse的蹤影。而C#上的"&&"和"||",在其他Language中,如PHP,名稱上又的確是"And"和"Or"(PS: PHP也有分"&"/"&&"和"|"/"||",但命名一樣。)
(http://www.w3schools.com/PHP/php_operators.asp),所以.NET新手或者會忽略箇中分別。

AndAlso和OrElse都是一種Conditional Operators,只會在有需要時才運算。

例如以這段Code為例,一定會出現錯誤: FormatException :Input string was not in a correct format.
因為在IF的部份,"a"不可能轉做Integer。
當然你可以分兩段IF...End IF,先TryParse,得到True之後才做Convert.ToInt32。但最好寫法就是把And轉成AndAlso。

轉成AndAlso後,程式會判斷第一部份是否True,然後才決定執行之後部份。
Visual Basic :
Dim s As String() = New String(3) {"1", "2", "3", "a"}
Dim i As Integer
For Each _s As String In s
If Integer.TryParse(_s, i) And Convert.ToInt32(_s) <= 3 Then
System.Diagnostics.Debug.Print(_s)
End If
Next

C# :
string[] s = new string[4] { "1", "2", "3", "a" };
int  i;
foreach (string _s in s)
{
if (int.TryParse(_s, out  i) & Convert.ToInt32(_s) <=3)
{ System.Diagnostics.Debug.Print(_s); }
}

And("&")和AndAlso("&&")的分別 :

And :
Visual Basic :
Dim a As Integer = 1
Dim b As Integer = 1
If a = 2 And b = Convert.ToInt32(Date.Now) Then '產生錯誤 : Invalid cast from 'DateTime' to 'Int32'.
Debug.Print("Hello!")
End If

C# :
int a= 1;
int b= 1;
if (a == 2 & b == Convert.ToInt32(DateTime.Now)) //產生錯誤 : Invalid cast from 'DateTime' to 'Int32'.
{ Console.WriteLine("Hello!");  } 

AndAlso :
Visual Basic :
Dim a As Integer = 1
Dim b As Integer = 1
If a = 2 AndAlso b = Convert.ToInt32(Date.Now) Then 
Debug.Print("Hello!") 'Output "Hello!"
End If

C# :
int a= 1;
int b= 1;
if (a == 2 && b == Convert.ToInt32(DateTime.Now))
{ Console.WriteLine("Hello!");  } //Output "Hello!"

Or("|")和OrElse("||")的分別 :

而OrElse的情況跟AndAlso相似。
AndAlso是只要任何一Part是False的話,都不再執行其餘的expression;
而OrElse則是只要前面的Boolean expression是True的話,後面的就不再運算,直接執行內裡的statement。

Or("|") :
Visual Basic :
Dim a As Integer = 1
Dim b As Integer = 1
If a = 1 Or b = Convert.ToInt32(Date.Now) Then '產生錯誤 : Invalid cast from 'DateTime' to 'Int32'.
Debug.Print("Hello!")
End If

C# :
int a= 1;
int b= 1;
if (a == 1 | b == Convert.ToInt32(DateTime.Now)) //產生錯誤 : Invalid cast from 'DateTime' to 'Int32'.
{ Console.WriteLine("Hello!");  } 


OrElse("||") :
Visual Basic :
Dim a As Integer = 1
Dim b As Integer = 1
If a = 1 OrElse b = Convert.ToInt32(Date.Now) Then 
Debug.Print("Hello!") 'Output "Hello!"
End If

C# :
int a= 1;
int b= 1;
if (a == 1 || b == Convert.ToInt32(DateTime.Now)) 
{ Console.WriteLine("Hello!");  } //Output "Hello!"

2 則留言:

  1. Based on .Net documentation,

    && = And
    & = Bitwise-And operations

    Please refer to :
    http://msdn.microsoft.com/en-us/library/2a723cdk(VS.80).aspx
    http://msdn.microsoft.com/en-us/library/sbf85k1c(VS.80).aspx

    回覆刪除
  2. Dim a As Integer = 1
    Dim b As Integer = 1
    If a = 2 AndAlso b = Convert.ToInt32(Date.Now)
    Then Debug.Print("Hello!") 'Output "Hello!" End If

    這段不會印出Hello吧,還敢說同事fresh耶...

    回覆刪除