Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>My question is, what happens when I set my variable to <strong>null</strong> inside a using block? Will it still get disposed properly?</p> </blockquote> <p>That depends on where your variable's declared. It will either be disposed of correctly, or your code won't compile in the first place.</p> <h1>If your variable is declared outside the using statement: Yes</h1> <pre><code>A a; using (a = new A()) { a = null; } </code></pre> <p>Yes, it will be disposed properly. For a simple test:</p> <p><img src="https://i.stack.imgur.com/mmLnN.png" alt="A variable is nulled within a using block and disposed of successfully"><br> <sup>My text colour scheme is <a href="http://studiostyl.es/schemes/ragnarok-grey" rel="nofollow noreferrer">Ragnarok Grey</a>.</sup></p> <p>Even with the assignment cleared, a reference to the <code>new A()</code> seems to have been maintained. <code>a</code> is disposed of at the end of the <code>using</code> statement as expected, even if it's nulled inside.</p> <p>In some versions of Visual Studio, this may result in <a href="http://msdn.microsoft.com/en-us/library/zhdyhfk6(v=vs.90).aspx" rel="nofollow noreferrer">Compiler Warning (level 2) CS0728</a>:</p> <blockquote> <p>Possibly incorrect assignment to local 'a' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.</p> </blockquote> <h1>If your variable declared within the using statement: N/A</h1> <pre><code>using (var a = new A()) { a = null; } </code></pre> <p>The above code won't compile. You're not permitted to assign to a <code>using</code> variable in the first place. The above code produces this compile error:</p> <blockquote> <p>Cannot assign to 'a' because it is a 'using variable'</p> </blockquote> <p>This is <a href="http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&amp;l=EN-US&amp;k=k(CS1656);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5)&amp;rd=true" rel="nofollow noreferrer">Compiler Error CS1656</a>.</p> <blockquote> <p>This error occurs when an assignment to variable occurs in a read-only context. Read-only contexts include <strong>foreach</strong> iteration variables, <strong>using</strong> variables, and fixed variables. To resolve this error, avoid assignments to a statement variable in <strong>using</strong> blocks, <strong>foreach</strong> statements, and <strong>fixed</strong> statements.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload