title: Why cannot return a nested ref return result if the inner method has another ref/out parameter?
body: <p>I thought I understood <a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/ref-returns" rel="nofollow noreferrer">limitations</a> of ref returns as well as escaping scopes.</p>
  <p>But now I found a case and I don't get why is it illegal. It seems that ref return cannot be used if the value to return comes from a nested method with any ref/out arguments.</p>
  <pre><code>public static ref T RefReturn&lt;T&gt;()
  {
      ref T result = ref GetResult&lt;T&gt;(normalParam: default); // compiles
      // ref T result = ref GetResult&lt;T&gt;(out bool outParam); // CS8157 at the return statement
      // ref T result = ref GetResult&lt;T&gt;(boxedParam: new StrongBox&lt;bool&gt;()); // a suboptimal workaround
  
      // ... code
  
      return ref result;
  }
  
  // the bodies below are just dummy examples
  private static ref T GetResult&lt;T&gt;(bool normalParam) =&gt; ref (new T[1])[0];
  
  // note that this method compiles in itself but unlike in case of the the previous method
  // it is illegal to return its result again
  private static ref T GetResult&lt;T&gt;(out bool outParam)
  {
      outParam = true;
      return ref (new T[1])[0];
  }
  
  // though a ref/out parameter does not work we can mimic something similar without
  // making it impossible to return its result from a caller
  private static ref T GetResult&lt;T&gt;(StrongBox&lt;bool&gt; boxedParam)
  {
      boxedParam.Value = true;
      return ref (new T[1])[0];
  }
  </code></pre>
  <p>I can live with the workaround but I don't understand how an independent by-ref argument makes this solution harmful. AFAIK they must not be dangerous to the inner ref result; otherwise, the inner method itself should trigger some error, too. Or am I wrong? What do I miss?</p>
  <p><a href="https://dotnetfiddle.net/iH3C3t" rel="nofollow noreferrer">Here</a> is the live example of the code sample above.</p>