using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleAppLevel2Class { public class ResourceManager { // Simulate handlers / pointers private IntPtr resourceHandle; // unmanaged resource private bool isDisposed = false; // placeholder to see the status of the unmanaged resource public ResourceManager() // constructor for ResourceManager Class // construct data { resourceHandle = (IntPtr)123456; // "unmanaged" memory space Console.WriteLine("Unmanaged allocated resource " + resourceHandle); } // Implement IDisposable Interface public void Dispose() { Dispose(true); GC.SuppressFinalize(this); // Prevent finanlizer from running if successful } // IDisposable // Make the implementation of Dispose protected // public private protected protected virtual void Dispose(bool disposing) { // nested if 1. do we need to run Finalizer 2. If we do need to run it, what handlers are marked to be disposed if (!isDisposed) { // true if (disposing) { // Free other managed objects } // disposing // free unmanaged objects if (resourceHandle != IntPtr.Zero) { Console.WriteLine("Unmanged memory released. "); resourceHandle = IntPtr.Zero; } isDisposed = true; } // (!isDisposed) } // Dispose() // Dispose of the unmanaged resource // Finalizer ~ResourceManager() { // Disposable Method return bool // true dispose of the unmanaged handle // false it leaves it in memory Dispose(false); } // Finalizer } // Resource Manager } // ns