What is the difference between these statements
obj=null and obj.dispose()

Answers were Sorted based on User's Feedback



What is the difference between these statements obj=null and obj.dispose() ..

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

What is the difference between these statements obj=null and obj.dispose() ..

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

What is the difference between these statements obj=null and obj.dispose() ..

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

What is the difference between these statements obj=null and obj.dispose() ..

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

Post New Answer

More C Sharp Interview Questions

How long can a string be c#?

0 Answers  


Can we assign null value to integer?

0 Answers  


What is delegates and events?

0 Answers  


What is difference between hashtable and dictionary in c#?

0 Answers  


Explain what a diffgram, and a good use for one Define diffgram? How it be used?

0 Answers   Siebel,






What is a string in c#?

0 Answers  


Why is lazy loading?

0 Answers  


how to add datarow to datatable

1 Answers  


Is it possible to have different access modifiers on the get/set methods of a property?

0 Answers  


What are tuples c#?

0 Answers  


What is manifest ?

3 Answers   Digital GlobalSoft,


Why to use “using” in c#?

0 Answers  


Categories