Saturday, February 17, 2007 3:56 AM
by
Paul Wu
Best Use of &&
You may have to written code that checks if an object is null before you use it. For example:
|
Rexx.Lexer.RexxCodeStatementList list = null; string name; if (list != null) { if (list.Count > 1) name =list[1].Name; } else { name = "-NA-"; } |
This can be abbreviated into the following code using the && operator:
|
Rexx.Lexer.RexxCodeStatementList list = null; string name; if (list != null && list.Count > 1) { name =list[1].Name; } else { name = "-NA-"; } |
The && (andalso in VB.NET) shortcircuit the if statment when false.