.net - Close and Dispose - which to call? -
having read threads is sqlcommand.dispose enough? , closing , disposing wcf service wondering classes such sqlconnection or 1 of several classes inheriting stream class matter if close dispose rather close?
i want clarify situation.
according microsoft guidelines, it's practice provide close
method suitable. here citation framework design guidelines
consider providing method
close()
, in additiondispose()
, if close standard terminology in area. when doing so, important makeclose
implementation identicaldispose
...
in of cases close
, dispose
methods equivalent. main difference between close
, dispose
in case of sqlconnectionobject
is:
an application can call
close
more 1 time. no exception generated.if called
dispose
methodsqlconnection
object state reset. if try call method on disposedsqlconnection
object, receive exception.
that said:
- if use connection object 1 time, use
dispose
. - if connection object must reused, use
close
method.
Comments
Post a Comment