Destructors (Poorly Named Finalizers)

Besides constructors, C# has the notion of destructors. Before going any further, let’s start with the mandatory warning. In the world of garbage collection, destructors only share their name with the concept known from C++. That is, they are totally different in behavior, despite their common syntax. To put this straight, consider the following piece of C++ code. Readers unfamiliar with C++ can skip the following example:

#include <stdio.h>class Destruct{public:    Destruct() {        printf("Constructing\n");    }    ~Destruct() {        printf("Destructing\n");    }};void Stack() {    printf("Stack - begin\n");    Destruct d;    printf("Stack - end\n");}void Heap() {    printf("Heap - begin\n");    Destruct ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.