Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does a using statement without variable do when disposing?

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

public class DbProviderConnection : IDisposable {     public DbConnection Connection { get; set; }     public DbTransaction Transaction { get; set; }      public DbTransaction BeginTransaction()     {         Transaction = Connection.BeginTransaction();         return Transaction;     }       //... and so on } 

Now i was thinkin to use it like this:

using (DbProviderConnection cnctn = _planDb.CreateOpenConnection()) {     using (cnctn.BeginTransaction())     {         //...         cnctn.Transaction.Commit();     } } 

My question is: Is the DbProviderConnection.Transaction.Dispose called?

like image 692
char m Avatar asked Feb 24 '14 12:02

char m


People also ask

Does using statement Call dispose?

The using statement guarantees that the object is disposed in the event an exception is thrown. It's the equivalent of calling dispose in a finally block.

How do you dispose of a variable?

Answers. We don't dispose the variables. In case if there are any unmanaged resources (or the ones which are inherited from IDisposable) can have Dispose() calls. In your case, you may have simple string or other primitive datatypes - these are not the ones which implement IDisposable interface.

What statement describes a Dispose method?

C# provides a special "using" statement to call Dispose method explicitly. using statement gives you a proper way to call the Dispose method on the object. In using statement, we instantiate an object in the statement. At the end of using statement block, it automatically calls the Dispose method.

What does the using statement do in C#?

The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations. The main goal is to manage resources and release all the resources automatically.


2 Answers

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(); } 
like image 187
Sergey Berezovskiy Avatar answered Sep 28 '22 04:09

Sergey Berezovskiy


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.

like image 20
Lee Avatar answered Sep 28 '22 06:09

Lee