Null question

  • Thread starter Thread starter Kimmo Laine
  • Start date Start date
K

Kimmo Laine

Hello,

what does the null assignment means in C#:

CMyClass mc = new CMyClass();
// Do something with mc

mc = null;

What does the "mc = null"-line actually means: is the mc, and the memory and
resources it has allocated, released?


thx

Kimmo Laine
 
Kimmo Laine said:
what does the null assignment means in C#:

CMyClass mc = new CMyClass();
// Do something with mc

mc = null;

What does the "mc = null"-line actually means: is the mc, and the memory and
resources it has allocated, released?

No - it means that the variable named "mc" has its value set to a null
reference.

If its previous value was the last "live" reference to an object, the
object is now eligible for garbage collection and will be collected
next time the garbage collector runs (on the generation the object is
part of, at least).

The important thing to realise here is that the value of mc *isn't* an
object - it's just a reference.
 
Back
Top