Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi In http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx, it is given that
relational (>) has higher priority than Equality (!=).

So following code must throw NullReferenceException:

C#
Form f = null;
if (f != null && f.Width > 10)
{
}

But it is not throwing any exception.
Please help me to understand.
Posted
Updated 4-Oct-14 16:55pm
v2
Comments
[no name] 4-Oct-14 9:51am    
First, != is inequality not equality. f is null so the f.Width is not evaluated, http://msdn.microsoft.com/en-us/library/2a723cdk.aspx
johnyroyan 4-Oct-14 9:55am    
but > must be evaluated before != ,no?
Dave Kreskowiak 4-Oct-14 10:01am    
NO! It's plainly obvious that is not the case! In your example, the two operations have nothing to do with each other.

What you're missing is that there is another operator in that expression: && (and).

What you're seeing is called "short circuiting". In an AND operation, each side of the expression is evaluated separately, but, if the left side expression evaluates to false the right side expression is not evaluated.

That's why you're not seeing a NullReferenceException.

C# does short circuiting automatically. VB.NET requires that you use the AndAlso and OrElse operators to do the same thing.


Read: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx
johnyroyan 4-Oct-14 10:12am    
thank you very much. i understood
Sergey Alexandrovich Kryukov 4-Oct-14 16:29pm    
Just a note: both C# and VB.NET do the same: explicit short-circuiting (what would be "automatic"?): in case of C#, you explicitly use '&&' or '||' instead of non-short Boolean '&' and '|', in VB.NET, you explicitly use AndAlso and OrElse. Same thing.

Please see Solution 1.

—SA

1 solution

It will not throw exception because you are using '&&' operator, which is the short-curcuit form of AND: if first Boolean operand is false, it is apparent that the result of AND is also false, so the second operator is not evaluated at all.

If you used '&' operator, it would throw exception.

What you have written is quite a valid known pattern to write shortcut check-ups without a need to use the second if. Basically, this is what the operators '||' and '&&' are designed for.

Be careful with the operands causing side effect (which can happen in case of Boolean function or property): the call may not be performed due to this short-circuit Boolean calculation.

—SA
 
Share this answer
 
v2
Comments
[no name] 4-Oct-14 16:48pm    
"&&" is not optimized, it is simply a convention, that as soon as a boolean operation is "solved", that the rest is obsolete...or am I wrong?
Zoltán Zörgő 4-Oct-14 17:25pm    
Not quite. "Short-circuit evaluation" is not a must - it is no convention. In many languages there is a possibility to enable or disable it (like in Delphi, see: http://www.delphibasics.co.uk/RTL.asp?Name=$B). But I don't know of any possibility to disable it in C# in case of logical && and || operators (see: http://msdn.microsoft.com/en-us/library/2a723cdk.aspx). Bitwise & and | are not short-circuit, since they can not be by definition.
[no name] 4-Oct-14 17:35pm    
Thank you very much for this info. But "&&" and "||" is clearly defined. "&&" is similar to multiplication "||" is addition.

Bitwise & or | is not really in discussion, even SA mentioned this ;)

Sorry for this incomplete explanation, but my english is weak :(
Zoltán Zörgő 4-Oct-14 18:04pm    
Well, as a logical value is actually a one bit integral value, you can use both &&|| and &| in case of boolean operands. So this is how you can influence the short-circuit effect.
[no name] 4-Oct-14 18:14pm    
&& and & || and |are completly different....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900