Friday, March 23, 2012

iDisposable

I have seen this done two different ways.

<code>

SQL =

New SqlConnection(strSQL)

CType

(SQL, IDisposable).Dispose()

</code>

or

<code>

SQL =New SqlConnection(strSQL)

sql.dispose()

</code>

Is there any difference between the two? If so, which way is considered to be best practice?

I dont know the exact differences here but in terms of best practise ive always been told that the using statement is best as it autodisposes at the end of execution:

using(SqlConnection conSQL = new SqlConnection(strSQL))

{

//do whatever

} // at this point the SqlConnection's dispose() method gets called automatically

|||first one is invocation through the interface. If the Dispose method of SqlConnection is declared private (Explicitly implemented), the only way to call it is through the interface.


void IDisposable.Dispose()
{
...whatever
}

Not much of performance difference. I also prefer using blocks.

No comments:

Post a Comment