.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 addition dispose(), if close standard terminology in area. when doing so, important make close implementation identical dispose ...

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 method sqlconnection object state reset. if try call method on disposed sqlconnection object, receive exception.

that said:

  • if use connection object 1 time, use dispose.
  • if connection object must reused, use close method.

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -