Short-Circuit Evaluation
noun
say it: “SHORT SER-kit ih-val-yoo-AY-shun” /ʃɔːrt ˈsɜːrkɪt ɪˌvæljuˈeɪʃən/
The way && and || stop checking as soon as the left side already decides the answer.
What is Short-Circuit Evaluation in C#?
Short-Circuit Evaluation — && and || stop as soon as the left side settles the answer: with &&, a false left side means the right side never runs. Put the safety check first, as here, where it stops a divide by zero.
int gamesPlayed = 0;
int wins = 0;
if (gamesPlayed > 0 && (decimal)wins / gamesPlayed > 0.5m)
{
Console.WriteLine("Winning record");
}
Console.WriteLine("No crash"); // No crash
In a sentence
- “Thanks to short-circuit evaluation, the division never ran when gamesPlayed was zero.”
Level: Apprentice · More C# terms at this level
Updated