using System;

class Ghost
{
    internal static int counter = 0;
    string name;

    private Ghost()
    {
        ++counter;
        name = "Gespenst" + counter;
    }

    ~Ghost()
    {
        --counter;
    }

    public static Ghost CreateGhost()
    {
        if (counter < 3)
            return new Ghost();
        else
            return null;
    }

    public void Howl()
    {
        Console.WriteLine("\t" + name + ": Huuhuu!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Ghost oGhost1 = Ghost.CreateGhost();
        Ghost oGhost2 = Ghost.CreateGhost();
        Ghost oGhost3 = Ghost.CreateGhost();
        Ghost oGhost4 = Ghost.CreateGhost();

        Console.WriteLine("Es spuken zur Zeit {0} Gespenster:\n",
                         Ghost.counter);

        if (oGhost1 != null) oGhost1.Howl();
        if (oGhost2 != null) oGhost2.Howl();
        if (oGhost3 != null) oGhost3.Howl();
        if (oGhost4 != null) oGhost4.Howl();

        oGhost2 = null;

        GC.Collect();
        GC.WaitForPendingFinalizers();  // auf Ausführung der Destruktoren warten

        Console.WriteLine();
        Console.WriteLine("Es spuken zur Zeit {0} Gespenster: \n",
                        Ghost.counter);

        if (oGhost1 != null) oGhost1.Howl();
        if (oGhost2 != null) oGhost2.Howl();
        if (oGhost3 != null) oGhost3.Howl();
        if (oGhost4 != null) oGhost4.Howl();

        Console.WriteLine();
    }
}
