title: Why can't c# use inline anonymous lambdas or delegates?
body: <p>I hope I worded the title of my question appropriately.</p>
  
  <p>In c# I can use lambdas (as delegates), or the older delegate syntax to do this:</p>
  
  <pre><code>Func&lt;string&gt; fnHello = () =&gt; "hello";
  Console.WriteLine(fnHello());
  
  Func&lt;string&gt; fnHello2 = delegate()
  {
      return "hello 2";
  };
  Console.WriteLine(fnHello2());
  </code></pre>
  
  <p>So why can't I "inline" the lambda or the delegate body, and avoid capturing it in a named variable (making it anonymous)?</p>
  
  <pre><code>// Inline anonymous lambda not allowed
  Console.WriteLine(
      (() =&gt; "hello inline lambda")()
  );
  
  // Inline anonymous delegate not allowed
  Console.WriteLine(
      (delegate() { return "hello inline delegate"; })()
  );
  </code></pre>
  
  <p>An example that works in javascript (just for comparison) is:</p>
  
  <pre><code>alert(
      (function(){ return "hello inline anonymous function from javascript"; })()
  );
  </code></pre>
  
  <p>Which produces the expected alert box.</p>
  
  <p><strong>UPDATE</strong>:
  It seems you <em>can</em> have an inline anonymous lambda in C#, if you cast appropriately, but the amount of ()'s starts to make it unruly.</p>
  
  <pre><code>// Inline anonymous lambda with appropriate cast IS allowed
  Console.WriteLine(
      ((Func&lt;string&gt;)(() =&gt; "hello inline anonymous lambda"))()
  );
  </code></pre>
  
  <p>Perhaps the compiler can't infer the sig of the anonymous delegate to know which Console.WriteLine() you're trying to call?  Does anyone know why this specific cast is required?</p>