what does a using statement without variable do when disposing?
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.
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();
}