Assigning the null Value

Learn how the null value can be assigned to a variable.

We'll cover the following

null value assignment

Assigning the null value to a variable of a reference type makes that variable stop referencing its current object.

the actual object becomes a candidate for finalization by the garbage collector. After all, not being referenced by any variable means that the object is not being used in the program at all.

Let’s look at an example where two variables are referencing the same object:

auto variable = new MyClass; 
auto variable2 = variable;

The following is a representation of the state of the memory after executing that code:

Get hands-on with 1000+ tech skills courses.