title: How to make multiplication operator (*) behave as short-circuit?
body: <p>I have lots of computations, specially multiplication, where first part is sometimes zero and I don't want to evaluate second operand in that case. There are at least two short-circuit operators in C#: <code>&amp;&amp;</code> and <code>||</code> which evaluate second operand only if necessary. I want to achieve similar behavior with multiplication operator. </p>
  
  <p>In <strong>.net</strong> you can't overload <code>&amp;&amp;</code> operator directly, but you can overload <code>&amp;</code> and <code>false</code> operators, so you can use your extension points to change behavior of short-circuit operator. You can find more details at this article <a href="http://blog.onderweg.eu/2010/11/csharp-overloading-short-circuit-operators/" rel="noreferrer">C# operator overloading: the ‘&amp;&amp;’ operator</a></p>
  
  <p><strong>Is there any means to achieve this or <em>similar</em> behavior for multiplication operator?</strong> </p>
  
  <p>This is a pure syntax question, because implementation is very simple. Next method achieves exactly what I want in terms of functionality: </p>
  
  <pre><code>public static double ShortCircuitMultiply(double val, Func&lt;double&gt; anotherValue)
  {
      var epsilon = 0.00000001;
      return Math.Abs(val) &lt; epsilon ? 0 : val * anotherValue();
  }
  </code></pre>
  
  <p>Note: this implementation is not full: in <strong>C#</strong> if you multiply <code>0.0</code> with 
    <code>Double.NaN</code>  or <code>Double.NegativeInfinity</code> or <code>Double.PositiveInfinity</code>, you'll get <code>NaN</code>, but in terms of <code>ShortCircuitMultiply</code> - only zero. Let's ignore this detail and it's really irrelevant in my domain. </p>
  
  <p>So now if I call it as <code>ShortCircuitMultiply(0.0, longOperation)</code> where <code>longOperation</code> is <code>Func&lt;double&gt;</code>, last term won't be evaluated and result of an operation will be effectively zero. </p>
  
  <p>The problem is, as I already stated, I would have lots of <code>ShortCircuitMultiply</code> calls and I want to make code more readable. I want code to be similar to <code>0.0 * longOperation()</code> if that is possible. </p>
  
  <hr>
  
  <p>Another note: I've tried to build wrapper on <code>double</code> and create implicit casting to double and also overload <code>*</code> operator. I understand, that this is probably redundant: I wanted to acheive readability, but trying to build <a href="http://en.wikipedia.org/wiki/KISS_principle" rel="noreferrer">yet another wrapper</a>. Anyway, next code  demonstrates my intent:</p>
  
  <pre><code>class MyDouble
  {
      double value;
      public MyDouble(double value)
      {
          this.value = value; 
      }
  
      public static MyDouble operator *(MyDouble left, MyDouble right) 
      {
          Console.WriteLine ("* operator call");
          return new MyDouble(left.value * right.value);
      }
  
      public static implicit operator double(MyDouble myDouble)
      {
          Console.WriteLine ("cast to double");
          return myDouble.value;
      }
  
      public static implicit operator MyDouble(double value)
      {
          Console.WriteLine ("cast to MyDouble");
          return new MyDouble(value);
      }
  }
  </code></pre>
  
  <p>Now if I go with:</p>
  
  <pre><code>MyDouble zero = 0;
  
  Console.WriteLine (zero * longOperation()); //longOperation is still Func&lt;double&gt;
  </code></pre>
  
  <p>I've receive:</p>
  
  <pre><code>cast to MyDouble            
  called longOperation        &lt;-- want to avoid this (it's printed from longOperation body)
  cast to double             
  cast to MyDouble
  * operator call
  cast to double   
  0
  </code></pre>
  
  <p>But as you can see, <code>longOperation</code> is evaluated long before overloaded operator is called, and I can't substitute one of the parameters with <code>Func</code> or <code>Expression</code> to make it lazy.</p>