Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>That is an interesting question... it works with <code>Decimal</code>, for example, but not <code>TimeSpan</code>, which are both proper .NET types (unlike <code>float</code> etc that are primatives) and both have an + operator. Curious!</p> <p>Of course, you can twist the arm with:</p> <pre><code>Money m3 = (Money)m1 + (Money)m2; </code></pre> <p>And it you just use <code>Nullable&lt;T&gt;</code> it'll work for free, of course - plus you get the compiler + runtime (boxing) support. Is there a reason not to use <code>Nullable&lt;T&gt;</code> here?</p> <p>I'll look at the spec; in the interim, you might think about promoting the operator to the <code>MyNullable&lt;T&gt;</code>; with regular <code>Nullable&lt;T&gt;</code>, the C# compiler provides "lifted" operators for those supported by the type, but you can't do that yourself. The best you can do is offer all the obvious ones and hope the type supports it ;-p To access operators with generics, see <a href="http://www.pobox.com/~skeet/csharp/miscutil/usage/genericoperators.html" rel="noreferrer">here</a>, available for free download <a href="http://www.pobox.com/~skeet/csharp/miscutil/" rel="noreferrer">here</a>.</p> <p>Note you'd probably want to apply the appropriate "lifted" checks - i.e.</p> <pre><code>x + y =&gt; (x.HasValue &amp;&amp; y.HasValue) ? new MyNullable&lt;T&gt;(x.Value + y.Value) : new MyNullable&lt;T&gt;(); </code></pre> <p><strong>Update</strong></p> <p>The different handling looks to relate to 14.7.4 (ECMA 334 v4) "Addition operator", where it is pre-defined for a range of types <em>including</em> decimal (so that was a bad test by me), since by 14.2.4 (same) "Binary operator overload resolution", the pre-defined operators do get special mention. I don't claim to understand it fully, though.</p>
 

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