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();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…