Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An exception should be an exception, not the usual program flow. But lets not care about that for now :)</p> <p>The direct answer to you question I believe is no, but you can always work yourself around the problem. I'm posting a small part of some of the ninja stuff I made when implementing unbounded integers (in effect a linked list of integers) which could help you.</p> <p>This is a very simplistic approach for doing checked addition manually if performance is not an issue. Is quite nice if you can overload the operators of the types, ie you control the types.</p> <pre><code>public static int SafeAdd(int left, int right) { if (left == 0 || right == 0 || left &lt; 0 &amp;&amp; right &gt; 0 || right &lt; 0 &amp;&amp; left &gt; 0) // One is 0 or they are both on different sides of 0 return left + right; else if (right &gt; 0 &amp;&amp; left &gt; 0 &amp;&amp; int.MaxValue - right &gt; left) // More than 0 and ok return left + right; else if (right &lt; 0 &amp;&amp; left &lt; 0 &amp;&amp; int.MinValue - right &lt; left) // Less than 0 and ok return left + right; else throw new OverflowException(); } </code></pre> <p>Example with your own types:</p> <pre><code>public struct MyNumber { public MyNumber(int value) { n = value; } public int n; // the value public static MyNumber operator +(MyNumber left, MyNumber right) { if (left == 0 || right == 0 || left &lt; 0 &amp;&amp; right &gt; 0 || right &lt; 0 &amp;&amp; left &gt; 0) // One is 0 or they are both on different sides of 0 return new MyNumber(left.n + right.n); // int addition else if (right &gt; 0 &amp;&amp; left &gt; 0 &amp;&amp; int.MaxValue - right &gt; left) // More than 0 and ok return new MyNumber(left.n + right.n); // int addition else if (right &lt; 0 &amp;&amp; left &lt; 0 &amp;&amp; int.MinValue - right &lt; left) // Less than 0 and ok return new MyNumber(left.n + right.n); // int addition else throw new OverflowException(); } // I'm lazy, you should define your own comparisons really public static implicit operator int(MyNumber number) { return number.n; } } </code></pre> <p>As I stated earlier, you will lose performance, but gain the exceptions.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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