Thread: StreamWriter

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    StreamWriter

    Hello everyone,


    For example, my class Foo wraps an object of StreamWriter, I must make Foo implements IDisposable and in the Dispose method of class Foo, invoke Dispose method of the StreamWriter object instance to release resource properly?

    Code:
    using System.IO;
    
    
    public class Foo : IDisposable
    {
        StreamWriter a;
    
        public Foo()
    	{
    
    	}
    
        public void Dispose
        {
            if (null != a)
            {
                a.Dispose();
            }
        }
    
    }

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I have no idea what you're asking this time, but as far as i know, you don't need to do this as garbage collection is automatic.

    Code:
    using System.IO
    
    public class Foo
    {
      StreamWriter a;
    
      public Foo()
      {
      }
    }
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Unless you are dealing with some unmanaged code inside that class. More info @ http://www.codeproject.com/KB/cs/gcresdealloc.aspx

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks DanFraser and GanglyLamb,


    One further question which is not answered is what is the relationship between Finalize method and destructor?

    Finalize will call destructor or vice versa? Or no relationship? GC will call Finalize, but I am not sure whether GC will call destructor as well?

    Quote Originally Posted by GanglyLamb View Post
    Unless you are dealing with some unmanaged code inside that class. More info @ http://www.codeproject.com/KB/cs/gcresdealloc.aspx

    regards,
    George

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by George2 View Post
    Thanks DanFraser and GanglyLamb,


    One further question which is not answered is what is the relationship between Finalize method and destructor?

    Finalize will call destructor or vice versa? Or no relationship? GC will call Finalize, but I am not sure whether GC will call destructor as well?




    regards,
    George
    http://www.c-sharpcorner.com/UploadF...structors.aspx
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Magos,


    I have learned that compiler will translate destructor into finalize method.

    Two further questions,

    1. Is it best practice to implement destructor or implement Finalize?

    2. What happens if we implement both destructor and Finalize? Which one will overwrite which one?


    regards,
    George

  7. #7
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    24

    Wink

    Streams are closed and then disposed/destroyed as they act allot like, if not identical to, threads.

    It is good practice to confirm changes to a stream and then save the stream prior to closing it and initializing the destructor.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks TheRaven,


    I agree and understand all of your points below, except "act allot like, if not identical to, threads" -- what do you mean?

    Quote Originally Posted by TheRaven View Post
    Streams are closed and then disposed/destroyed as they act allot like, if not identical to, threads.

    It is good practice to confirm changes to a stream and then save the stream prior to closing it and initializing the destructor.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Streamwriter not writing
    By deviousdexter in forum C# Programming
    Replies: 6
    Last Post: 11-24-2008, 09:39 AM
  2. file close issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-11-2008, 12:20 AM