what does a using statement without variable do when disposing?

前端 未结 4 826
感动是毒
感动是毒 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.
    

提交回复
热议问题