13.14. Object Deletion and Garbage Collection

In the C++ language, there is a delete operator that allows the programmer to explicitly control when a dynamically allocated object is no longer needed, and its memory can therefore be recycled. This is both a blessing and a curse! It's a blessing because it gives a C++ programmer very tight control over his/her memory resources in a program. But if a C++ programmer forgets to recycle his/her objects, the program can literally run out of memory—this is known as a memory leak.

With C#, however, there is no delete operator: anything dynamically created—all reference types as opposed to value types—is a candidate for C# garbage collection when all references to it (handles) have been eliminated. Like ...

Get Beginning C# 2008 Objects: From Concept to Code 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.