左邊遇到 False 後,右邊就不會評估了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
bool SecondOperand()
{
    Console.WriteLine("Second operand is evaluated.");
    return true;
}

bool a = false && SecondOperand();
Console.WriteLine(a);
// Output:
// False

bool b = true && SecondOperand();
Console.WriteLine(b);
// Output:
// Second operand is evaluated.
// True

參考自官方文件的說明:


文章作者: Michael

上次更新: 2023-12-12

授權條款: 保留所有權利