2 <clause number="15.13" title="The using statement">
3 <paragraph>The using statement obtains one or more resources, executes a statement, and then disposes of the resource. <grammar_production><name><non_terminal where="15.13">using-statement</non_terminal></name> : <rhs><keyword>using</keyword><terminal>(</terminal><non_terminal where="15.13">resource-acquisition</non_terminal><terminal>)</terminal><non_terminal where="15">embedded-statement</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="15.13">resource-acquisition</non_terminal></name> : <rhs><non_terminal where="15.5.1">local-variable-declaration</non_terminal></rhs><rhs><non_terminal where="14.14">expression</non_terminal></rhs></grammar_production></paragraph>
4 <paragraph>A resource is a class or struct that implements System.IDisposable, which includes a single parameterless method named Dispose. Code that is using a resource can call Dispose to indicate that the resource is no longer needed. If Dispose is not called, then automatic disposal eventually occurs as a consequence of garbage collection. </paragraph>
5 <paragraph>If the form of <non_terminal where="15.13">resource-acquisition</non_terminal> is <non_terminal where="15.5.1">local-variable-declaration</non_terminal> then the type of the <non_terminal where="15.5.1">local-variable-declaration</non_terminal> must be System.IDisposable or a type that can be implicitly converted to System.IDisposable. If the form of <non_terminal where="15.13">resource-acquisition</non_terminal> is expression then this expression must be System.IDisposable or a type that can be implicitly converted to System.IDisposable. </paragraph>
6 <paragraph>Local variables declared in a <non_terminal where="15.13">resource-acquisition</non_terminal> are read-only, and must include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (via assignment or the ++ and --operators) or pass them as ref or out parameters. </paragraph>
7 <paragraph>A using statement is translated into three parts: acquisition, usage, and disposal. Usage of the resource is implicitly enclosed in a try statement that includes a finally clause. This finally clause disposes of the resource. If a null resource is acquired, then no call to Dispose is made, and no exception is thrown. </paragraph>
8 <paragraph>A using statement of the form <code_example><![CDATA[
9 using (R r1 = new R()) {
12 ]]></code_example>is precisely equivalent to <code_example><![CDATA[
18 if (r1 != null) ((IDisposable)r1).Dispose();
20 ]]></code_example></paragraph>
21 <paragraph>A <non_terminal where="15.13">resource-acquisition</non_terminal> may acquire multiple resources of a given type. This is equivalent to nested using statements. A using statement of the form <code_example><![CDATA[
22 using (R r1 = new R(), r2 = new R()) {
26 ]]></code_example>is precisely equivalent to: <code_example><![CDATA[
27 using (R r1 = new R())
28 using (R r2 = new R()) {
32 ]]></code_example>which is, by expansion, precisely equivalent to: <code_example><![CDATA[
41 if (r2 != null) ((IDisposable)r2).Dispose();
45 if (r1 != null) ((IDisposable)r1).Dispose();
47 <table_line></table_line>
48 ]]></code_example></paragraph>