title: Will C# compiler and optimization break this code?
body: <p>Given the following C# code inside a function:</p>
  
  <pre><code>....
  var documentCollection =
      client.CreateDocumentCollectionQuery("dbs/" + database.Id)
          .Where(c =&gt; c.Id == DocumentCollectionName)
          .AsEnumerable()
          .FirstOrDefault();
  
  if (documentCollection == null)
  {
      documentCollection =
          await
          client.CreateDocumentCollectionAsync(
              "dbs/" + database.Id,
              new DocumentCollection { Id = DocumentCollectionName });
  }
  
  return client;
  </code></pre>
  
  <p>Note: I'm <em>not</em> returning the <code>documentCollection</code>, I just need it to be initialized, if not already ( the <code>CreateDocumentCollectionAsync</code> call ). So - after the <code>if</code> block , <code>documentCollection</code> becomes an unused variable. </p>
  
  <p>Now - ReSharper proposes to optimize this to:</p>
  
  <pre><code>var documentCollection =
      client.CreateDocumentCollectionQuery("dbs/" + database.Id)
          .Where(c =&gt; c.Id == DocumentCollectionName)
          .AsEnumerable()
          .FirstOrDefault()
      ?? await
          client.CreateDocumentCollectionAsync(
              "dbs/" + database.Id,
              new DocumentCollection { Id = DocumentCollectionName });
  </code></pre>
  
  <p>And indicates now that <code>documentCollection</code> is an unused variable. </p>
  
  <p><strong>My question:</strong> will C# code optimization or a 'release' build completely remove this line of code and result in the <code>CreateDocumentCollectionAsync</code> to never fire?</p>
  
  <p>The C# optimization course taught me that 'release' builds garbage collect variables as soon as they're not needed 'down the line' in the function, whereas debug builds don't do that (for debugging purposes). </p>
  
  <p>I'm now wondering whether it's so eager even that it optimizes away an unused variable assignment (which triggers an operation in the background).</p>