object = null

  • Thread starter Thread starter Roel
  • Start date Start date
R

Roel

Hello

I'm pretty new to C#...

Once you don't need an object anymore, do need to kill it or not ?

--> myObject = null;

I know we are using the garbage collector but I don't know how it exactly
works...

Regards
Roel
 
You set the reference to the object to null. Then the garbage collector
will find that the object isn't referenced to anything and clear it.

myObject = null is one way to do it. But for temporary objects that will
fall out of scope you don't need to do even that.

Some classes have a Close() method. And if it has, you should use it
before it falls out of scope. Usually complex classes using resources
that will clean up itself in the Close method.
 
Hi,
Some classes have a Close() method. And if it has, you should use it
before it falls out of scope. Usually complex classes using resources
that will clean up itself in the Close method.

Some objects implements IDisposable interface.
These objects should be disposed by:
if( obj is IDisposable ) {
obj.Dispose();
}
obj=null;

Marcin

PS: Don't give a name "object" to any variables.
 
Back
Top