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

What does the keyword virtual mean in the method definition?

3 Answers   Ipog Software, Satyam, Visual Soft,


How does the XmlSerializer work ?

1 Answers  


How does bitwise work?

0 Answers  


How?s method overriding different from overloading?

2 Answers   Visual Soft,


Can we overload indexer in c#?

0 Answers  






Where static variables are stored?

0 Answers  


why C#.net doesnot support prcedures and why it supports structs,functions

1 Answers  


What is console based application?

0 Answers  


Any .net training centers available near hitech city , hyderabad?

2 Answers  


What?s the difference between System.String and System.StringBuilder classes?

1 Answers  


What is lazy loading c#?

0 Answers  


When Doveloped C#

2 Answers  


Categories