Idisposable interface

You can ( must ) implement the iDisposable interface if you work with unmanaged code or native resources. The garbage collector cleans up all managed resources and is very effective in this.

Unmanaged code: basically all code what doesn't run in the .Net framework.
Native resources: connections to files ( filestreams ), databases etc.

Use objects wich implement the iDisposable interface with the using statement: 

using( myclass a = new myclass)

Example iDisposable implementation

public class DisposeExample
    // A base class that implements IDisposable.
    // By implementing IDisposable, you are announcing that
    // instances of this type allocate scarce resources.
    public class MyResource: IDisposable
        // Track whether Dispose has been called.
        private bool disposed = false;

        // The class constructor.
        public MyResource()

        // Implement IDisposable.
        // Do not make this method virtual.
        // A derived class should not be able to override this method.
        public void Dispose()

        // Dispose(bool disposing) executes in two distinct scenarios.
        // If disposing equals true, the method has been called directly
        // or indirectly by a user's code. Managed and unmanaged resources
        // can be disposed.
        // If disposing equals false, the method has been called by the
        // runtime from inside the finalizer and you should not reference
        // other objects. Only unmanaged resources can be disposed.
        private void Dispose(bool disposing)
            // Check to see if Dispose has already been called.
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                    // Dispose managed resources.

                // Note disposing has been done.
                disposed = true;

    public static void Main()
        // Insert code here to create
        // and use the MyResource object.




Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!