what is the difference between finally and dispose methods?

Answer Posted / monika

The important difference is timing. Finalize is called
after the .NET garbage collector runs. That can take a
while, depending on how frequently you allocate memory and
in what generation the object lives. Dispose() is the
standardized way to free up the unmanaged resources your
object uses well before the finalizer gets a chance to do
so. A Bitmap would be a good example, it takes very few
managed resources but can eat a huge chunk of unmanaged
memory, depending on the size of the bitmap. If you don't
call Dispose on it after you're done using it, your app can
potentially consume large amounts of unused memory for an
extended amount of time.

While Finalize is called automatically, Dispose() isn't.
You must call it explicitly or you can use the V2.0 "using"
statement in VB.NET or C#. Or the "stack allocation"
syntax in C++/CLI. Using "using" is preferred, it ensures
Dispose() is called, even if there's an exception. When
Dispose() runs, you won't need the finalizer anymore so
call GC.SuppressFinalize(). That makes the finalizer
thread running a lot leaner too.

You don't have to implement Dispose() if your class doesn't
consume unmanaged resources. If it has a member that
implements Dispose(), you'll need to implement Dispose()
too so you can call that member's Dispose() method.

Is This Answer Correct ?    53 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a void c#?

658


what is method overloading in c#?

743


Which controls do not have events?

738


is it possible to access a remote web service Without UDDI?

752


What is difference between array and list in c#?

616


What is the importance of closing an ado.net application?

663


What is uint16?

681


What does convert toint32 mean?

697


What is delegation in oops?

711


How to put assembly in gac?

698


Hello! How to do this: "Create manifest utility intended for creating update content files. Application should take a set of files as input parameter and generate XML based manifest file as output one." I use C# and vs.net 2003. It's urgent! Help please, thanks. Mayana

1688


What is the use of ienumerable in c#?

721


Where static variables are stored?

650


How do you inherit a class into other class in c#?

668


What does console mean c#?

672