Resource Management in C++: Listing 6

Scope-bound objects in C#.

using System;

class ScopeBoundExample : System.IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Releasing resource.");
    }

    static void Main(string[] args)
    {
        try
        {
            using (ScopeBoundExample sbe = new ScopeBoundExample())
            {
                Console.WriteLine("Inside the using block. Exception to be thrown.");
                throw new SystemException();
            }
        }
        catch (Exception)
        {
            // exception handling follows...
        }
    }
}

About the Author

Diego Dagum is a software architect and developer with more than 20 years of experience. He can be reached at email@diegodagum.com.