Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The type of the conditional expression has to be inferred as a whole - and lambda expressions always have to be converted to a specific delegate or expression tree type.</p> <p>In your latter two examples, the compiler knows what it's trying to convert the lambda expression to. In the first example, it tries to work out the type of the whole conditional expression first.</p> <p>A cast in one of the branches would be enough though:</p> <pre><code>protected override Func&lt;Stream&gt; GetStream() { return someBool ? (Func&lt;Stream&gt;) (() =&gt; EmbeddedResourceExtractor.GetFile("SomeFile1.ext")) : () =&gt; EmbeddedResourceExtractor.GetFile("SomeFile2.ext"); } </code></pre> <p>Sergio's fix (now deleted, but included below) will work <em>if</em> you were happy to evaluate <code>someBool</code> at the time the function is called:</p> <pre><code>protected override Func&lt;Stream&gt; GetStream() { return () =&gt; someBool ? EmbeddedResourceExtractor.GetFile("SomeFile1.ext") : EmbeddedResourceExtractor.GetFile("SomeFile2.ext"); } </code></pre> <p>Depending on timing, there are all kinds of different ways of fixing the example you've actually given, e.g.</p> <pre><code>protected override Func&lt;Stream&gt; GetStream() { string name = someBool ? "SomeFile1.ext" : "SomeFile2.ext"; return () =&gt; EmbeddedResourceExtractor.GetFile(name); } </code></pre> <p>I'm guessing your real code is more complicated though.</p> <p>It's a shame in some ways that C#'s type inference can't be more powerful - but it's already pretty complicated.</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. 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