Click here to Skip to main content
15,914,163 members
Home / Discussions / C#
   

C#

 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 2:08
George_George26-Nov-08 2:08 
GeneralRe: implement IDisposable issue Pin
leppie26-Nov-08 2:18
leppie26-Nov-08 2:18 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 2:25
George_George26-Nov-08 2:25 
GeneralRe: implement IDisposable issue Pin
leppie26-Nov-08 2:41
leppie26-Nov-08 2:41 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 3:16
George_George26-Nov-08 3:16 
GeneralRe: implement IDisposable issue Pin
leppie26-Nov-08 3:23
leppie26-Nov-08 3:23 
GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 3:31
George_George26-Nov-08 3:31 
AnswerRe: implement IDisposable issue Pin
Scott Dorman26-Nov-08 16:18
professionalScott Dorman26-Nov-08 16:18 
When you say "destructor" are you talking about writing a finalizer (~ClassName) method? If so, then you generally don't need to do this. Just because you implement Dispose does not mean you need to implement a finalizer.

The Dispose() method is called by another programmer when they are done using your object.

Yes, you normally would call GC.SuppressFinalizer(this) in your Dispose method. In the case of a static class there is no "this" object. Normally static classes do not hold any internal state and would not actually need to do any object disposal of their own. In your case, since this is a singleton class you will have some internal state however you are implementing the dispose pattern because you are managing another disposable class. In this case you actually don't need to implement the full dispose pattern and can get away with doing everything in the simple Dispose() method.

Again, normally the disposed field is an instance field as the pattern really only applies to non-static classes.

There are a few things wrong, ignoring the fact that this is a static class. If it were not a static class, it should be implmented as:

public class StreamWrapper : IDisposable
{
   private StreamWriter currentLogStream;
   private bool disposed; // no need to initialize here as the default is false

   public void Dispose()
   {
      this.Uninitialize();
   }
 
   protected virtual void Dispose(bool disposing)
   {
      if (!disposed)
      {
         if (disposing)
         {
            if (currentLogStream != null)
            {
               currentLogStream.Dispose();
            }
         }
      }
      disposed = true;
   }
 
   public void Uninitalize()
   {
      Dispose(true);
      GC.SuppressFinalize(this);
   }
}
Take a look at this article[^] for more details on how to implement dispose properly.

Scott Dorman
Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA

[Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

GeneralRe: implement IDisposable issue Pin
George_George26-Nov-08 20:58
George_George26-Nov-08 20:58 
GeneralRe: implement IDisposable issue Pin
Scott Dorman27-Nov-08 2:49
professionalScott Dorman27-Nov-08 2:49 
QuestionHow edit my URL on button click Pin
Exelioindia26-Nov-08 1:48
Exelioindia26-Nov-08 1:48 
AnswerRe: How edit my URL on button click Pin
Simon P Stevens26-Nov-08 1:56
Simon P Stevens26-Nov-08 1:56 
AnswerRe: How edit my URL on button click Pin
Dominic Goulet1-Dec-08 1:09
Dominic Goulet1-Dec-08 1:09 
QuestionExhange Server 2007 Webservice Pin
boiDev26-Nov-08 1:33
boiDev26-Nov-08 1:33 
Questioncopying files fast from 1 drive to other Pin
sumit703426-Nov-08 0:59
sumit703426-Nov-08 0:59 
AnswerRe: copying files fast from 1 drive to other Pin
Guffa26-Nov-08 1:14
Guffa26-Nov-08 1:14 
GeneralRe: copying files fast from 1 drive to other Pin
sumit703426-Nov-08 1:21
sumit703426-Nov-08 1:21 
AnswerRe: copying files fast from 1 drive to other Pin
Simon P Stevens26-Nov-08 1:16
Simon P Stevens26-Nov-08 1:16 
AnswerRe: copying files fast from 1 drive to other Pin
Shyam Bharath26-Nov-08 1:33
Shyam Bharath26-Nov-08 1:33 
Questionshowing a linklabel in a messagebox? Pin
lane0p226-Nov-08 0:53
lane0p226-Nov-08 0:53 
AnswerRe: showing a linklabel in a messagebox? Pin
Pedram Behroozi26-Nov-08 1:08
Pedram Behroozi26-Nov-08 1:08 
QuestionCode to find the csc.exe Pin
tonyjsebastian126-Nov-08 0:53
tonyjsebastian126-Nov-08 0:53 
AnswerRe: Code to find the csc.exe Pin
Shyam Bharath26-Nov-08 1:02
Shyam Bharath26-Nov-08 1:02 
AnswerRe: Code to find the csc.exe Pin
Pedram Behroozi26-Nov-08 1:02
Pedram Behroozi26-Nov-08 1:02 
AnswerRe: Code to find the csc.exe Pin
Mirko198026-Nov-08 2:15
Mirko198026-Nov-08 2:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.