George George

George George

  • NA
  • 778
  • 0

implement IDisposable issue

Nov 26 2008 7:59 AM

Hello everyone,

I have a static class which wraps a file stream object. And I want to implement the

Dispose pattern. I define an uninitialize method in the static class which call Dispose

explicitly with parameter value true. Here is my code,

My questions are,

1. Normally there should be a destructor in a class which calls Dispose with parameter

value false, but for a static class, there is no concept like destructor, how to implement

calling Dispose with false parameter?

2. When the Dispose method without parameter will be called?

3. Is it correct to call GC.SuppressFinalize(this)? Since for a static class, there is no "this"

object?

4. Correct to define disposed as static field?

5. Are there anything wrong with my code?

[Code]
        static private StreamWriter currentLogStream;

        private static bool disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private static void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (false == disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    // Dispose managed resources.
                    currentLogStream.Dispose();
                }
            }
            disposed = true;
        }

        public static void Uninitialize()
        {
            Dispose(true);
        }
[/Code]

thanks in advance,
George


Answers (5)