netFramwor and Dispose

G

Guest

..net Frame work
1. Dispose Method what it does ?
A. who its call / when it calls ?
B. Is it fire automatically ?
c. When dispose method is call what it does ?
D. Release a Object from memory or release refrence from memory ?
E. If it release object from memory then what GC does ?
2. Which class can have dispose method ?
class which is member of IDispose Inteface
3. Where we should use abstract / Interface ?
 
W

Woody

1. Dispose Method what it does ?
It is a member of the IDisposable interface and it used to free
unmanaged resources (windows handles, files, ...)

A. who its call / when it calls ?
It is called explicitly by the users of the class that implements it. A
call to the Dispose method it is propagated to all the parent types of
this class.

B. Is it fire automatically ?
It can also called by the runtime inside a finalizer method

c. When dispose method is call what it does ?
It is implementation dependent. If you develop a class that uses
unmanaged resources (eg, handles), you should free that resources
inside the IDisposable.Dispose method (eg. calling CloseHandle()).

D. Release a Object from memory or release refrence from memory ?
Releases an (unmanaged) object from memory at specific time

E. If it release object from memory then what GC does ?
GC frees unreferenced managed objects. GC has no knowledge of
unmanaged resources

2. Which class can have dispose method ?
class which is member of IDispose Inteface
Classes that create unmanaged resources should implement IDisposable
interface.
 
G

Guest

Woody said:
1. Dispose Method what it does ?
It is a member of the IDisposable interface and it used to free
unmanaged resources (windows handles, files, ...)

A. who its call / when it calls ?
It is called explicitly by the users of the class that implements it. A
call to the Dispose method it is propagated to all the parent types of
this class.

B. Is it fire automatically ?
It can also called by the runtime inside a finalizer method

c. When dispose method is call what it does ?
It is implementation dependent. If you develop a class that uses
unmanaged resources (eg, handles), you should free that resources
inside the IDisposable.Dispose method (eg. calling CloseHandle()).

D. Release a Object from memory or release refrence from memory ?
Releases an (unmanaged) object from memory at specific time

E. If it release object from memory then what GC does ?
GC frees unreferenced managed objects. GC has no knowledge of
unmanaged resources

2. Which class can have dispose method ?
class which is member of IDispose Inteface
Classes that create unmanaged resources should implement IDisposable
interface.

Great,
Thanks


Woody said:
1. Dispose Method what it does ?
It is a member of the IDisposable interface and it used to free
unmanaged resources (windows handles, files, ...)

Here what do u mean of unmanage resources ?
bcoz if we are using C# in that case we are implicitly using managecode
,I think in that case we wont required dispose method bcoz GC will take care
of these things.
Yes we can also use unmanage code in C# , but for that we need to
explicitly compile these class as in unsafe mode , I think in that case
only dispose method will work ?




A. who its call / when it calls ?
It is called explicitly by the users of the class that implements it. A
call to the Dispose method it is propagated to all the parent types of
this class.
c. When dispose method is call what it does ?
It is implementation dependent. If you develop a class that uses
unmanaged resources (eg, handles), you should free that resources
inside the IDisposable.Dispose method (eg. calling CloseHandle()).

In (A .) You are saying that its called explicitly ? if it call explicitly
how we can call
how we can call finalize method ?
Is there any finalize method , I think finalize is part of
gc.suppressfinalize().
What happend when we call object=null ?
1.1 It will also release resources ?
1.2 It will called finalize / dispose method ?

2.1 What is diffrence between finalize and dispose method ?
2.2 who will call to whome ?
2.3 finalize call dispose / dispose call finalize ?
2.4 how it will called (finalize/dispose) ?



Thanks in advance
 
C

Chris Lyon [MSFT]

--------------------
Here what do u mean of unmanage resources ?
bcoz if we are using C# in that case we are implicitly using managecode
,I think in that case we wont required dispose method bcoz GC will take care
of these things.
Yes we can also use unmanage code in C# , but for that we need to
explicitly compile these class as in unsafe mode , I think in that case
only dispose method will work ?

By unmanaged resources, we mean file handles, bitmaps, database collections, essentially any resource outside the scope of the runtime. What you're describing is unsafe
code, which is a different concept.

In (A .) You are saying that its called explicitly ?

Yes, Dispose is called explicitly by the user code.
how we can call finalize method ?

You can't. The finalizer is called by the GC at some undetermined time after the object has been collected.
What happend when we call object=null ?
1.1 It will also release resources ?
1.2 It will called finalize / dispose method ?

Setting an object to null does not do anything, except remove the reference to the object in memory. See http://weblogs.asp.net/clyon/archive/2004/12/01/273144.aspx.
2.1 What is diffrence between finalize and dispose method ?
2.2 who will call to whome ?
2.3 finalize call dispose / dispose call finalize ?
2.4 how it will called (finalize/dispose) ?

For answers to these questions, please see the Dispose Pattern Guidelines:
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp

Further reading regarding the use of Dispose:
http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx


Hope that helps!
-Chris

--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 

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