Can “using” with more than one resource cause a resource leak?

前端 未结 5 1416
情书的邮戳
情书的邮戳 2020-12-04 15:10

C# lets me do the following (example from MSDN):

using (Font font3 = new Font(\"Arial\", 10.0f),
            font4 = new Font(\"Arial\", 10.0f))
{
    // Use         


        
5条回答
  •  天命终不由人
    2020-12-04 15:32

    No.

    The compiler will generate a separate finally block for each variable.

    The spec (§8.13) says:

    When a resource-acquisition takes the form of a local-variable-declaration, it is possible to acquire multiple resources of a given type. A using statement of the form

    using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) statement 
    

    is precisely equivalent to a sequence of nested using statements:

    using (ResourceType r1 = e1)
       using (ResourceType r2 = e2)
          ...
             using (ResourceType rN = eN)
                statement
    

提交回复
热议问题