Confusion in Dispose() and Nothing ?

R

Rahul Arora

Hi!..All,

I have a great confusion about the Dispose() method and Nothing. Can
anybody tell me in detail how the two method work? what will happen to
my object after i dispose it? will i be able to use it ? and what after
making it nothing ? i 've recently come across a very strange behaviour
of the Dispose method which i'll share with u guys later.

Rahul Arora
 
?

=?iso-8859-1?Q?Lasse=20V=e5qs=e6ther=20Karlsen?=

Hello Rahul,
Hi!..All,

I have a great confusion about the Dispose() method and Nothing. Can
anybody tell me in detail how the two method work? what will happen to
my object after i dispose it? will i be able to use it ? and what
after making it nothing ? i 've recently come across a very strange
behaviour of the Dispose method which i'll share with u guys later.

Rahul Arora

Dispose is a method, Nothing is a value, equivalent to null in C#.

What Dispose does differs from object to object but its main purpose is to
clean up unmanaged resources, like file handles, socket handles, etc.
 
R

Rahul Arora

Yes, Agreed that it cleans up the unmanaged resources. but see a
situation. I created a Datatable. I filled it up using a dataadapter,
there after i accessed it's elements. and i dispose it at last. After
disposing it i tried accessing it's elements but it were accessible. If
dispose method clears the memory occupied by the objects, then how
could it be possible to access the elements after disposing the
datatable.?

Rahul Arora
 
M

Miha Markic [MVP C#]

Hi Rahul,

Dispose doesn't clear the managed memory as it can't - usually it just
releases references to managed instances and perhaps calls Dispose on them -
it is up to the programer what to do within Dispose method.
 
S

Sahil Malik [MVP C#]

This depends on the object you are talking about. Generally Dispose is used
for resource cleanup - but it is only as good as the implementation of
Dispose is.

Say for instance, SqlConnection.Dispose will close the underlying connection
and clear the stateful information such as connection string. So a Disposed
connection cannot be reopened.

SqlConnection.Close - only marks it as closed and makes it available for
pooling. Thus it can be reopened.

Setting SqlConnection to Nothing leaves it for the Garbage Collector to come
and pick it up - which means it may not be available for pooling until it is
GC'ed (4-10 minutes?).

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top