1.12. Garbage Collection

We do not explicitly delete objects allocated through the new expression. Rather, these objects are cleaned up by garbage collection in the runtime environment. The garbage collection algorithm recognizes when an object on the managed heap is no longer referenced. That object is marked as available for collection.

When we allocate a reference type on the managed heap, such as the following array object:

int [] fib =
    new int[6]{ 1,1,2,3,5,8 };

the heap object is recognized as having an active reference. In this example, the array object is referred to by fib.

Now let's initialize a second array handle with the object referred to by fib:

int [] notfib = fib;

The result is a shallow copy. Rather than notfib addressing ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.