Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'll have to instantiate one of the methods as a specific compatible delegate type. There's just no way around it. This will, unfortunately, be a little more verbose than you're looking for:</p> <pre><code>(a ? new Action&lt;T1, T2, T3, T4&gt;(f1) : f2)(a, long, parameter, list); </code></pre> <p>You're going to have to make the parameters involved explicit, whether that's by using an <code>Action</code> (or <code>Func</code>) generic overload that fits or declaring your own delegate type.</p> <p>This comes down to type resolution. Given the expression:</p> <pre><code>condition ? tVal : fVal </code></pre> <p>The compiler doesn't look for common assignment-compatible ancestors for <code>tVal</code> and <code>fVal</code> (and, even if it did, the litany of different delegate types to which each might be valid could be <em>huge</em>); if there isn't assignment compatibility between the types of <code>tVal</code> and <code>fVal</code> in either direction, the compiler makes you be explicit as to what you want.</p> <p>For what it's worth, you should be aware that taking this approach will allocate a new delegate to either <code>f1</code> or <code>f2</code> every time this method is called, then that delegate will be invoked, then discarded. I bring this up only because delegate invocation is slower than ordinary early-bound (or even virtual) method invocation. It may well not be a consideration and the tradeoff might be worth it, but it's still worth <em>knowing</em>.</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