Home All Groups Group Topic Archive Search About

Confusion in Dispose() and Nothing ?

Author
12 Jan 2006 10:39 AM
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

Author
12 Jan 2006 11:08 AM
Lasse V=e5qs=e6ther Karlsen
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Author
12 Jan 2006 11:31 AM
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
Author
12 Jan 2006 11:50 AM
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.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Rahul Arora" <rk.ra***@yahoo.com> wrote in message
news:1137065507.694894.231580@g49g2000cwa.googlegroups.com...
> 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
>
Author
12 Jan 2006 12:35 PM
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
----------------------------------------------------------------------------



Show quote
"Rahul Arora" <rk.ra***@yahoo.com> wrote in message
news:1137062352.939101.214260@g44g2000cwa.googlegroups.com...
> 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
>

AddThis Social Bookmark Button