Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In most cases, <code>&amp;&amp;</code> and <code>||</code> are preferred over <code>&amp;</code> and <code>|</code> because the former are short-circuited, meaning that the evaluation is canceled as soon as the result is clear. </p> <p>Example:</p> <pre><code>if(CanExecute() &amp;&amp; CanSave()) { } </code></pre> <p>If <code>CanExecute</code> returns <code>false</code>, the complete expression will be <code>false</code>, regardless of the return value of <code>CanSave</code>. Because of this, <code>CanSave</code> is not executed.</p> <p>This is very handy in the following circumstance:</p> <pre><code>string value; if(dict.TryGetValue(key, out value) &amp;&amp; value.Contains("test")) { // Do Something } </code></pre> <p><code>TryGetValue</code> returns <code>false</code> if the supplied key is not found in the dictionary. Because of the short-circuiting nature of <code>&amp;&amp;</code>, <code>value.Contains("test")</code> is only executed, when <code>TryGetValue</code> returns <code>true</code> and thus <code>value</code> is not <code>null</code>. If you would use the <em>bitwise AND</em> operator <code>&amp;</code> instead, you would get a <code>NullReferenceException</code> if the key is not found in the dictionary, because the second part of the expression is executed in any case.</p> <p>A similar but simpler example of this is the following code (as mentioned by TJHeuvel):</p> <pre><code>if(op != null &amp;&amp; op.CanExecute()) { // Do Something } </code></pre> <p><code>CanExecute</code> is only executed if <code>op</code> is not <code>null</code>. If <code>op</code> is <code>null</code>, the first part of the expression (<code>op != null</code>) evaluates to <code>false</code> and the evaluation of the rest (<code>op.CanExecute()</code>) is skipped.</p> <p>Apart from this, technically, they are different, too:<br> <code>&amp;&amp;</code> and <code>||</code> can only be used on <code>bool</code> whereas <code>&amp;</code> and <code>|</code> can be used on any integral type (<code>bool</code>, <code>int</code>, <code>long</code>, <code>sbyte</code>, ...), because they are bitwise operators. <code>&amp;</code> is the <em>bitwise AND</em> operator and <code>|</code> is the <em>bitwise OR</em> operator. </p> <p>To be very exact, in C#, those operators (<code>&amp;</code>, <code>|</code> [and <code>^</code>]) are called "Logical operators" (see the <a href="http://msdn.microsoft.com/en-us/library/ms228593.aspx">C# spec</a>, chapter 7.11). There are several implementations of these operators:</p> <ol> <li>For integers (<code>int</code>, <code>uint</code>, <code>long</code> and <code>ulong</code>, chapter 7.11.1):<br> They are implemented to compute the bitwise result of the operands and the operator, i.e. <code>&amp;</code> is implement to compute the bitwise logical <code>AND</code> etc.</li> <li>For enumerations (chapter 7.11.2):<br> They are implemented to perform the logical operation of the underlying type of the enumeration.</li> <li>For bools and nullable bools (chapter 7.11.3 and 7.11.4):<br> The result is not computed using bitwise calculations. The result is basically looked up based on the values of the two operands, because the number of possibilities is so small.<br> Because both values are used for the lookup, this implementation isn't short-circuiting.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload