Note to self: C# and logical shortcircuiting
C# (.NET) do short-circuit logical expressions; i.e.:
1)
(A || B)
==> If A is true, then only A is evaluated.
2)
(C && D)
==> If C is false, then only C is evaluated.
This means that, provided that A is true, expression 1 is true even though B would throw an exception if evaluated.
An example use of expression 2:
if ((filename != "") && (new FileInfo(filename).Exists)) //file exists
If filename == “”, the Exists-statement would throw an exception. However, since the first expression (…!=…) is false, the Exists-statement is never evaluated, and the exception doesn’t occur.
Tags: .NET, C#, programming
