Diff Between Setting an object to NULL and Dispose()

  • Thread starter Thread starter Dixon
  • Start date Start date
D

Dixon

wats the Diff Between Setting an object to NULL and calling the
Dispose() method for that object?
 
1.
DBConnection conn = new DBConnection(); // consturct one
DBConnection object
conn=null; // there is no reference to the consturcted object, and the
resource managed by the DBConnection object will be released when GC
starts, maybe a few minutes later, maybe a long time later.

2.
DBConnection conn = new DBConnection(); // consturct one
DBConnection object
conn.dispose; // the conn still refer to the consturcted object, but
the resource managed by the DBConnection object is released right now.
 
DBConnection con = new DBConnection();
DBConnection a = new DBConnection();
//At this stage both objects do hav a refrence,and their refrences are
held by the objects con and a

DBConnection obj;
DBConnection b;
//The objects obj and b hav nothing referred to, so wat about the
resources of these objects???

//So if they dont hav a reference is it equivalent to
con =null;
//So ive set con to null , so no reference here and their resources?
wat about them??
//At this stage is obj, b and con have no reference so wat about their
resources?

a.dispose();
//If i use dispose with object a then the reference is still there, but
the resource is released? is that right?
 
DBConnection con = new DBConnection();
DBConnection a = new DBConnection();
//At this stage both objects do hav a refrence,and their refrences are
held by the objects con and a

Here you have created two new DBConnection objects by virtue of using
the new keyword.
DBConnection obj;
DBConnection b;
//The objects obj and b hav nothing referred to, so wat about the
resources of these objects???

Here you have not created any objects but two variables that can later
refer to objects, but the objects themselves, have not been created
yet. Because they have not been created yet, there are no resources
used.
//So if they dont hav a reference is it equivalent to
con =null;

No it's not, because when you declared con, you alse created a new
object using the new keyword.
//So ive set con to null , so no reference here and their resources?
wat about them??

When you set con to null all you did was point con to nothing. The
original object still exists and its resources are still in use. Only
when the GC gets around to collecting it, will they be gone. There is
no way to know when this will happen.
//At this stage is obj, b and con have no reference so wat about their
resources?

Again, because you never created an object for obj and b, there were no
resources used. For con, the resources will be cleaned up by the GC
when it runs, but you dont know when that will be.
a.dispose();
//If i use dispose with object a then the reference is still there, but
the resource is released? is that right?

By calling a.dispose, you are explicitly releasing the resources used
by a, but there is still a reference. Although the GC may still
collect it.
 
That was very kind of you, Dunaway. Your answer really cleared the
doubts i had for long time....thank you soo much
 
Back
Top