What is the difference between these statements
obj=null and obj.dispose()
Answers were Sorted based on User's Feedback
Answer / sanjeev
obj=null
when we use obj=null then this object has existence in
memory with no any value.
obj.dispose()
when we use obj.dispose() then the clr release the memory
and there are no any existence of that object into the
memory. so it will be better to use obj.dispose()
Note:- By default it is automatically disposed by the clr
when there are no any references in the left
Is This Answer Correct ? | 8 Yes | 0 No |
Answer / pushparaj pentakota
obj=null is remove the reference to object only but that
object will be existed but obj.dispose() is clear the
reference and as well as clear the object also from the
memory.so obj.dispose() is best to use but in some cases not.
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / dhinaharan
obj.dispose has to be used and not obj=null, bcoz JIT
compiler is optimizing compiler, and setting a local
variable or parameter to null is the same as not
referencing the variable at all.
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / kishoreg
This is not a good and safe practice to call a Dispose.
In "Dot Net" it is automatically handle by Garbage
collection. It is the responsibility of GC to call the
Dispose and clear the memory.. but it is not sure that when
GC will call the Dispose.
so in that case we can forcefuly call it.
Consider one case when we forcefully destroy the object and
after some time i will try to use the same object at that
time u will get the error msg.
so better to object = null;
other material:
http://blogs.msdn.com/irenak/archive/2006/10/05/SYSK-
212_3A00_--Does-.NET-Garbage-Collector-call-Dispose_3F00_--
Dispose-_1320_-Best-Practices_2E00_.aspx
Is This Answer Correct ? | 4 Yes | 4 No |
Whats the use of string builder?
Why do we need constructor in c#?
Why are mutable structs evil?
Can we inherit a class with private constructor in c#?
What is the use of GC.KeepAlive Method?
Can a structure inherit a class.
Explain About .Net remoting
Is there throws keyword in c#?
Oops concepts ?
2 Answers Accenture, Digital GlobalSoft,
Why do we need to call CG.SupressFinalize?
Is equal in c#?
What are the basics of c#?