what does a using statement without variable do when disposing?

前端 未结 4 824
感动是毒
感动是毒 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:15

    Yeah, the Dispose will be called. the using statement only works with disposable objects. Like this:

    using (DbProviderConnection cnctn = _planDb.CreateOpenConnection())
    {
        using (cnctn.BeginTransaction())
        {
            // ...
            cnctn.Transaction.Commit();
        } // Here BeginTransaction.Dispose() is called.
    } // Here DbProviderConnection.Dispose() is called.
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-17 09:18

    From C# Specification 8.13 using statement defined as

    using-statement:
       using (resource-acquisition) embedded-statement
    

    Where resource-acquisition is

    resource-acquisition:
        local-variable-declaration
        expression
    

    In first case you have using which acquires resource via local variable declaration. In second case resource is acquired via expression. So, in second case resouce will be result of cnctn.BeginTransaction() call, which is DbTransaction from your DbProviderConnection class. Using statement disposes its resource after usage. So, yes, DbProviderConnection.Transaction.Dispose() will be called.

    UPDATE: According to same article, your second using block will be translated to

    DbTransaction resource = cnctn.BeginTransaction();
    try
    {
        //...
        cnctn.Transaction.Commit();
    }
    finally 
    {
       if (resource != null) 
          ((IDisposable)resource).Dispose();
    }
    
    0 讨论(0)
  • 2020-12-17 09:23

    The object that BeginTransaction returns, is what will be disposed.

    BeginTransaction returns a DbTransaction so that is what will be disposed

    0 讨论(0)
提交回复
热议问题