title: C# 8 Using Declaration Scope Confusion
body: <p>With the new C# 8 Using Declaration Syntax, what is containing scope of a second consecutive using statement?</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>Previous to C# 8, having a consecutive using statement like:</p>
  
  <pre><code>using(var disposable = new MemoryStream())
  {
      using(var secondDisposable = new StreamWriter(disposable))
      {}
  }
  </code></pre>
  
  <p>would expand to something like the following  (<a href="https://github.com/dotnet/csharplang/blob/master/spec/statements.md#the-using-statement" rel="noreferrer">My Source</a>):</p>
  
  <pre><code>MemoryStream disposable = new MemoryStream();
  try {
      {
          StreamWriter secondDisposable = new StreamWriter(disposable);    
          try{
              {}
          }
          finally {
              if(secondDisposable != null) ((IDisposable)secondDisposable).Dispose();
          }
      }
  }
  finally {
      if(disposable != null) ((IDisposable)disposable).Dispose();
  }
  </code></pre>
  
  <p><strong>I know that there are two other possible expansions but they all are roughly like this</strong></p>
  
  <p>After upgrading to C# 8, Visual studio offered a Code Cleanup suggestion that I'm not certain I believe is an equivalent suggestion.</p>
  
  <p>It converted the above consecutive using statement to:</p>
  
  <pre><code>using var disposable = new MemoryStream();
  using var secondDisposable = new StreamWriter(disposable);
  </code></pre>
  
  <p>To me this changes the second's scope to the same scope as the first.  In this case, It would probably coincidentally dispose of the streams in the correct order, but I'm not certain I like to rely on that happy coincidence.</p>
  
  <p><strong>To be clear on what VS asked me to do:  I first converted the inner (which made sense because the inner was still contained in the outer's scope). Then I converted the outer (which locally made sense because it was still contained in the method's scope).  The combination of these two clean ups is what I'm curious about.</strong></p>
  
  <p>I also recognize that my thinking on this could be slightly (or even dramatically) off, but as I understand it today, this doesn't seem correct.  What is missing in my assessment?  Am I off base? </p>
  
  <p>The only thing I can think of is that there is some sort of an implicit scope inserted in the expansion for everything following a declaration statement.</p>