Diferences between null,close and dispose
-
Ok heres a question its roaming my mind. Whats the difference doing for example. SqlConnection con= new SqlConnection(connection_string); con.close(); con.dispoe(); and con= null; AFAIK i think this. Close just clsoes the connection and can be opened again, dipose frees the resources so it cant be opened again, must declare again and null makes the GC get it faster. With this object i see it clearly but its the same for others? DataSet ds= new DataSet(); Lets take about datasets, if i do a ds.dispose() its completely erased? and what about if i do ds=null; whats will do if i return the dataset in a method and later i do a dispose or a null in the finally clausule. Ty for ur time.
-
Ok heres a question its roaming my mind. Whats the difference doing for example. SqlConnection con= new SqlConnection(connection_string); con.close(); con.dispoe(); and con= null; AFAIK i think this. Close just clsoes the connection and can be opened again, dipose frees the resources so it cant be opened again, must declare again and null makes the GC get it faster. With this object i see it clearly but its the same for others? DataSet ds= new DataSet(); Lets take about datasets, if i do a ds.dispose() its completely erased? and what about if i do ds=null; whats will do if i return the dataset in a method and later i do a dispose or a null in the finally clausule. Ty for ur time.
Not really. SqlConnection's Close and Dispose do the same thing, if you look at the code, Dispose calls Close. null doesn't necessarily make the GC get it faster, in general, the GC is intelligent enough to figure out that that object has no live references. The only place where setting to null is recommended is if that object is contained within a long living object. So the answer is 1. Close and Dispose do the same thing. It has nothing to do with GC. 2. Setting to null doesn't make the GC get it faster in most cases. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Ok heres a question its roaming my mind. Whats the difference doing for example. SqlConnection con= new SqlConnection(connection_string); con.close(); con.dispoe(); and con= null; AFAIK i think this. Close just clsoes the connection and can be opened again, dipose frees the resources so it cant be opened again, must declare again and null makes the GC get it faster. With this object i see it clearly but its the same for others? DataSet ds= new DataSet(); Lets take about datasets, if i do a ds.dispose() its completely erased? and what about if i do ds=null; whats will do if i return the dataset in a method and later i do a dispose or a null in the finally clausule. Ty for ur time.
The previous post is not entirely correct.
Close()
andDispose()
do not do the same thing with respect to a SqlConnection. It is true thatDispose()
will close the connection if theConnectionState
is Open, but the converse is not true: ifClose()
is called, theSqlConnection
is not disposed, and your initial assertion is correct - the connection may be re-opened. In general, the following should ideally be true for disposable types (the concept of Close is not covered, since it doesn't really apply to the discussion, and may mean different things to different types - file streams, database connections, sockets etc.): 1.Dispose()
should be responsible for cleaning up unmanaged resources and indicating to the GC that finalization is suppressed for the instance (suppressing finalization explicitly avoids the default two-pass collection of collectible objects). The value ofDispose()
is that unmanaged resources can be reclaimed immediately (or nearly so), without waiting for the GC. Suppressing finalization for the object serves to streamline the garbage collection process so that it takes only one pass to reclaim the managed resources used by the object instead of two passes (one to finalize, one to reclaim). 2. Setting an object tonull
just drops a reference to the object, essentially setting it adrift in the (managed) heap until the GC executes and begins cleaning it up. There is no guarantee about when the resources for that object will be reclaimed. For more info, look here.[^] Hope this helps.The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
-
Ok heres a question its roaming my mind. Whats the difference doing for example. SqlConnection con= new SqlConnection(connection_string); con.close(); con.dispoe(); and con= null; AFAIK i think this. Close just clsoes the connection and can be opened again, dipose frees the resources so it cant be opened again, must declare again and null makes the GC get it faster. With this object i see it clearly but its the same for others? DataSet ds= new DataSet(); Lets take about datasets, if i do a ds.dispose() its completely erased? and what about if i do ds=null; whats will do if i return the dataset in a method and later i do a dispose or a null in the finally clausule. Ty for ur time.
The general rule of thumb is that anytime you call
Dispose
any unmanaged resources that were allocated are now unavailable so doing anything further with the object is risky. Since the object is kind of in an invalid state using the object beyond theDispose
. It should also be noted thatDispose
can happen independant ofObject.Finalize
.Dispose
is an interface so an object can deallocate resources and not meant as the general "death of the object". There is a whole side topic of the GC that should be discussed outside of the thread if you really care. My general practice is when I'm done with an object that has to manage limited resources, I callDispose
(I usually try to place this in afinally
block) and then set it tonull
so if anyone "accidently" tries to reuse the object it will throw aNullReferenceException
. -
The previous post is not entirely correct.
Close()
andDispose()
do not do the same thing with respect to a SqlConnection. It is true thatDispose()
will close the connection if theConnectionState
is Open, but the converse is not true: ifClose()
is called, theSqlConnection
is not disposed, and your initial assertion is correct - the connection may be re-opened. In general, the following should ideally be true for disposable types (the concept of Close is not covered, since it doesn't really apply to the discussion, and may mean different things to different types - file streams, database connections, sockets etc.): 1.Dispose()
should be responsible for cleaning up unmanaged resources and indicating to the GC that finalization is suppressed for the instance (suppressing finalization explicitly avoids the default two-pass collection of collectible objects). The value ofDispose()
is that unmanaged resources can be reclaimed immediately (or nearly so), without waiting for the GC. Suppressing finalization for the object serves to streamline the garbage collection process so that it takes only one pass to reclaim the managed resources used by the object instead of two passes (one to finalize, one to reclaim). 2. Setting an object tonull
just drops a reference to the object, essentially setting it adrift in the (managed) heap until the GC executes and begins cleaning it up. There is no guarantee about when the resources for that object will be reclaimed. For more info, look here.[^] Hope this helps.The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
turbochimp wrote: It is true that Dispose() will close the connection if the ConnectionState is Open, but the converse is not true: if Close() is called, the SqlConnection is not disposed I don't understand. If Dispose calls Close(), then Dispose and Close must do the same thing. What do you mean by "the SqlConnection is not disposed" ? If you can call Open() on a connection on which you've called Close, you can as well call it after calling Dispose(). Dispose doesn't do anything extra, as you can see with a tool like Reflector. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
turbochimp wrote: It is true that Dispose() will close the connection if the ConnectionState is Open, but the converse is not true: if Close() is called, the SqlConnection is not disposed I don't understand. If Dispose calls Close(), then Dispose and Close must do the same thing. What do you mean by "the SqlConnection is not disposed" ? If you can call Open() on a connection on which you've called Close, you can as well call it after calling Dispose(). Dispose doesn't do anything extra, as you can see with a tool like Reflector. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Dispose calls Close, but Close doesn't call Dispose, so the two don't do the same thing! You can do this:
SqlConnection conn = new SqlConnection(...); conn.Open(); conn.Close(); conn.Open(); conn.Dispose();
but you will get an exception with this:SqlConnection conn = new SqlConnection(...); conn.Open(); conn.Dispose(); conn.Open();
Regards, mav -
Ok heres a question its roaming my mind. Whats the difference doing for example. SqlConnection con= new SqlConnection(connection_string); con.close(); con.dispoe(); and con= null; AFAIK i think this. Close just clsoes the connection and can be opened again, dipose frees the resources so it cant be opened again, must declare again and null makes the GC get it faster. With this object i see it clearly but its the same for others? DataSet ds= new DataSet(); Lets take about datasets, if i do a ds.dispose() its completely erased? and what about if i do ds=null; whats will do if i return the dataset in a method and later i do a dispose or a null in the finally clausule. Ty for ur time.
-
Dispose calls Close, but Close doesn't call Dispose, so the two don't do the same thing! You can do this:
SqlConnection conn = new SqlConnection(...); conn.Open(); conn.Close(); conn.Open(); conn.Dispose();
but you will get an exception with this:SqlConnection conn = new SqlConnection(...); conn.Open(); conn.Dispose(); conn.Open();
Regards, mavWell, if you have two methods A() and B(), and A does nothing but call B(), then they both do the same thing. If A called B and B called A, you'll get mutual recursion :) That apart, you're right, I missed the fact that Dispose clears the ConnectionString property after calling Close(). This piece of code works fine.
SqlConnection conn = new SqlConnection(...); conn.Open(); conn.Dispose(); conn.ConnectionString = "..."; conn.Open(); conn.Dispose();
I still do maintain that they both essentially do the same thing, I don't know why the FCL designers added code to clear the connection string.. If you ask me, Close and Dispose should do the same thing. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Well, if you have two methods A() and B(), and A does nothing but call B(), then they both do the same thing. If A called B and B called A, you'll get mutual recursion :) That apart, you're right, I missed the fact that Dispose clears the ConnectionString property after calling Close(). This piece of code works fine.
SqlConnection conn = new SqlConnection(...); conn.Open(); conn.Dispose(); conn.ConnectionString = "..."; conn.Open(); conn.Dispose();
I still do maintain that they both essentially do the same thing, I don't know why the FCL designers added code to clear the connection string.. If you ask me, Close and Dispose should do the same thing. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
I think you are confused about the intended uses of
Dispose()
andClose()
. They are very different. I would encourage you to read the same article I suggested in my original post. I would also encourage you to look further into what actually happens whenDispose()
is called on aSqlConnection
(or any type that implementsIDisposable
, if it's implemented correctly). Specifically, inSqlConnection.Dispose()
, you will (or should) see a call tobase.Dispose(disposing)
. If you had taken the time to inspect the base class (System.ComponentModel.Component
), you would have seen that there's a little more to the disposal of theSqlConnection
instance than you indicated in either of your posts.The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
-
I think you are confused about the intended uses of
Dispose()
andClose()
. They are very different. I would encourage you to read the same article I suggested in my original post. I would also encourage you to look further into what actually happens whenDispose()
is called on aSqlConnection
(or any type that implementsIDisposable
, if it's implemented correctly). Specifically, inSqlConnection.Dispose()
, you will (or should) see a call tobase.Dispose(disposing)
. If you had taken the time to inspect the base class (System.ComponentModel.Component
), you would have seen that there's a little more to the disposal of theSqlConnection
instance than you indicated in either of your posts.The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
I admit that I was wrong with the semantics of
Close()
andDispose
with respect toSqlConnection
. What I'm still not convinced about is thatClose
is supposed to be a "customized" Dispose, as this[^] and this[^] link mention, [EDIT] but in the case of SqlConnection, they act differently.[/EDIT] Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
I admit that I was wrong with the semantics of
Close()
andDispose
with respect toSqlConnection
. What I'm still not convinced about is thatClose
is supposed to be a "customized" Dispose, as this[^] and this[^] link mention, [EDIT] but in the case of SqlConnection, they act differently.[/EDIT] Regards Senthil _____________________________ My Blog | My Articles | WinMacroWhile I would have to place myself in the "Richter" camp on this as well, I understand the point these articles are trying to make, and I don't know if I really disagree with their implementations, except possibly from an aesthetic point of view. My thinking is: 1.
Dispose()
has a clear definition and purpose regardless of type. If the type has unmanaged resources, and it may be important to release those resources in a time-critical manner, then the type will likely benefit from aDispose()
method. 2.Close()
has a somewhat less well-defined purpose that is affected by the type on which it is implemented. It has a high-level meaning that applies in most places (i.e. "To stop or obstruct some kind of operation or behavior") and means the same thing to most people, but the specifics of how to "Close" an instance of a type are potentially specific to the type itself, as are the rules about re-opening the instance, how closing affects state, etc. There are clearly some cases whereClose()
andDispose()
might effectively mean the same thing, as the first article suggests, but there are obviously others (e.g.SqlConnection
) where the intent of the developers was to make the methods behave differently. Both are okay in my book, but what can't get overlooked is that whileClose()
may or may not be implemented on a disposable type, theIDisposable
interface ( thusDispose()
) *must* be implemented, or unexpected results may ensue. In those cases whereClose()
andDispose()
are interchangeable, the word "Close" may be considered more "user friendly" or less confusing than "Dispose", and so a case can be made for implementing aClose()
method that simply masks theDispose()
method. In general though, I'd prefer a more expository API (one that had both methods marked public, if the developers really felt that strongly about it), but that's just me, and the decision really boils down to developer preference and the consuming client's perceived level of understanding. Have a good one.The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’