title: Initializing and testing an object inside an if expression to reduce it's lifespan
body: <p>I'm wondering if I can initialize an object inside an object inside the test expression of an <code>if</code> statement and test whether or not it's null there.</p>
  
  <p>Trying it like this gives a syntax error.</p>
  
  <pre><code>public void test()
  {
     if ((ArtItem artItem=_context.ArtItems.FirstOrDefault(ai=&gt;ai.Id==1)) != null)
     {
  
     }
  }
  </code></pre>
  
  <p>Also, if such a thing were possible, would it be more efficient than doing this?</p>
  
  <pre><code>public void test()
  {
      ArtItem artItem = _context.ArtItems.FirstOrDefault(ai =&gt; ai.Id == 1);
              if (artItem != null)
      {
  
      }
  }
  </code></pre>
  
  <p>I assume yes, since it would technically be initialized and destroyed within the scope of the if statement, not the entire function.</p>
  
  <p><a href="https://i.stack.imgur.com/2JQSN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2JQSN.png" alt="enter image description here"></a></p>