Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>personally I'd avoid the short circuit from operators and just let the methods chain it:</p> <pre><code>public static int CompareChain&lt;T&gt;(this int previous, T a, T b) { if (previous != 0) return previous; return Comparer&lt;T&gt;.Default.Compare(a,b); } </code></pre> <p>use like so:</p> <pre><code>int a = 0, b = 2; string x = "foo", y = "bar"; return a.Compare(b).CompareChain(x,y); </code></pre> <p>can be inlined by the JIT so it can perform just as well as short circuiting built into the language without messing about with more complexity.</p> <p>In response to your asking whether the above 'structure' can apply to more than just comparisons then yes it can, by making the choice of whether to continue or not explict and controllable by the user. This is inherently more complex but, the operation is more flexible so this is unavoidable.</p> <pre><code>public static T ElseIf&lt;T&gt;( this T previous, Func&lt;T,bool&gt; isOK Func&lt;T&gt; candidate) { if (previous != null &amp;&amp; isOK(previous)) return previous; return candidate(); } </code></pre> <p>then use like so</p> <pre><code>Connection bestConnection = server1.GetConnection() .ElseIf(IsOk, server2.GetConnection) .ElseIf(IsOk, server3.GetConnection) .ElseIf(IsOk, () =&gt; null); </code></pre> <p>This is maximum flexibility in that you can alter the <em>IsOk</em> check at any stage and are entirely lazy. For situations where the is OK check is the same in every case you can simplify like so and entirely avoid extensions methods.</p> <pre><code>public static T ElseIf&lt;T&gt;( Func&lt;T,bool&gt; isOK IEnumerable&lt;Func&lt;T&gt;[] candidates) { foreach (var candidate in candidates) { var t = candidate(); if (isOK(t)) return t; } throw new ArgumentException("none were acceptable"); } </code></pre> <p>You could do this with linq but this way gives a nice error message and allows this </p> <pre><code>public static T ElseIf&lt;T&gt;( Func&lt;T,bool&gt; isOK params Func&lt;T&gt;[] candidates) { return ElseIf&lt;T&gt;(isOK, (IEnumerable&lt;Func&lt;T&gt;&gt;)candidates); } </code></pre> <p>style which leads to nice readable code like so:</p> <pre><code>var bestConnection = ElseIf(IsOk, server1.GetConnection, server2.GetConnection, server3.GetConnection); </code></pre> <p>If you want to allow a default value then:</p> <pre><code>public static T ElseIfOrDefault&lt;T&gt;( Func&lt;T,bool&gt; isOK IEnumerable&lt;Func&lt;T&gt;&gt;[] candidates) { foreach (var candidate in candidates) { var t = candidate(); if (isOK(t)) return t; } return default(T); } </code></pre> <p>Obviously all the above can very easily be written using lambdas so your specific example would be:</p> <pre><code>var bestConnection = ElseIfOrDefault( c =&gt; c != null &amp;&amp; !(c.IsBusy || c.IsFull), server1.GetConnection, server2.GetConnection, server3.GetConnection); </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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