Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would say that the correct answer to the question is given by E.Lippert in <a href="https://stackoverflow.com/questions/4965576/why-cant-an-anonymous-method-be-assigned-to-var">SO Why can't an anonymous method be assigned to var?</a></p> <p>But let us play with your example a little:</p> <pre><code>Func&lt;Func&lt;int&gt;&gt; f = () =&gt; { int i = 0; return () =&gt; i; }; Func&lt;int&gt; x = Test(f); //it compiles OK </code></pre> <p>No problem in type inference with your <code>Func&lt;T&gt; Test&lt;T&gt;(Func&lt;Func&lt;T&gt;&gt; func)</code> here. The problem is hidden in that you use an anonymous lambda expression, the type of which cannot be inferred. Try this:</p> <pre><code>var f = () =&gt; { int i = 0; return () =&gt; i; }; </code></pre> <p>It gives <a href="http://msdn.microsoft.com/en-us/library/bb384140%28v=vs.90%29.aspx" rel="nofollow noreferrer">Compiler Error CS0815</a>, saying</p> <blockquote> <p>Cannot assign lambda expression to an implicitly-typed local variable</p> </blockquote> <p>and the explanation is:</p> <blockquote> <p>An expression that is used as the initializer for an implicitly typed variable must have a type. Because anonymous function expressions, method group expressions, and the null literal expression do not have a type, they are not appropriate initializers. An implicitly typed variable cannot be initialized with a null value in its declaration, although it can later be assigned a value of null.</p> </blockquote> <p>Now let's try another thing: </p> <pre><code>var x = Test(() =&gt; { Func&lt;int&gt; f = () =&gt; 0; return f; }); </code></pre> <p>It compiles as well. So the problem with your original example was actually with this line:</p> <pre><code>return () =&gt; i; </code></pre> <p>We can go further and according to what Eric Lippert says in his answer provide another function to wrap this:</p> <pre><code>static Func&lt;T&gt; GetFunc&lt;T&gt;(Func&lt;T&gt; f) { return f; } </code></pre> <p>Now we can rewrite your code like:</p> <pre><code>var x = Test(() =&gt; { int i = 0; return GetFunc(() =&gt; i); }); </code></pre> <p>And it works as well.</p> <p>However, as far as I understand, this all is an overhead and you should just provide an explicit type. While these workarounds are suitable, when you need to have a lambda, returning an object of anonymous type.</p>
    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.
    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