what is the difference between finally and dispose methods?
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / sunil
finally will execute after the try block. It must execute
if there is any exception caught in the catch blobk or not.
Dispose method is the last method executed in the c# program
| Is This Answer Correct ? | 12 Yes | 16 No |
Answer / m.m.ganesh
compulsory finally will execute after try and catch
block.Dispose method is used to clean up the
objects.Dispose methos will call in finally method to clean
up the objects
| Is This Answer Correct ? | 6 Yes | 19 No |
Example of value type and refarnce type
Why do we use public static void main in c#?
Can I call a virtual method from a constructor/destructor?
What are functions c#?
What is system console writeline in c#?
Is is possible to force garbage collector to run?
How can we main a Class as base class,i.e not inherited further?{please Dont tell using Sealed class,any other way}
6 Answers ACB Group, IBM, Tech Mahindra,
Why to use lock statement in c#?
What is Lambda Expression?
What can you do as a .net developer?
Can fields inside a class be virtual?
What is dapper in c#?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)