what does a using statement without variable do when disposing?

前端 未结 4 828
感动是毒
感动是毒 2020-12-17 09:01

I\'ve always used using with variable and assignment. Now i have like this a class DbProviderConnection:

public class DbProviderConnection : IDisposable
{
           


        
4条回答
  •  一生所求
    2020-12-17 09:16

    From the specification:

    8.13 The using statement

    A using statement of the form

    using (ResourceType resource = expression) statement
    

    when ResourceType is a nullable value type or a reference type other than dynamic, the expansion is

    {
        ResourceType resource = expression;
        try {
            statement;
        }
        finally {
            if (resource != null) ((IDisposable)resource).Dispose();
        }
    }
    

    A using statement of the form

    using (expression) statement
    

    has the same three possible expansions...The resource variable is inaccessible in, and invisible to, the embedded statement.

    Therefore the object returned from cnctn.BeginTransaction() will be disposed when the block exits, but is not accessible inside the associated block.

提交回复
热议问题