C Board  

Go Back   C Board > General Programming Boards > C# Programming

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 04-15-2008, 03:42 AM   #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
George2 is offline  
Old 04-15-2008, 04:14 AM   #2
Registered User
 
Join Date: Jun 2003
Posts: 90
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
DanFraser is offline  
Old 04-15-2008, 05:16 AM   #3
and the Hat of Clumsiness
 
GanglyLamb's Avatar
 
Join Date: Oct 2002
Posts: 1,101
Unless you are dealing with some unmanaged code inside that class. More info @ http://www.codeproject.com/KB/cs/gcresdealloc.aspx
GanglyLamb is offline  
Old 04-15-2008, 07:12 AM   #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
George2 is offline  
Old 04-15-2008, 07:16 AM   #5
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
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.
Magos is offline  
Old 04-15-2008, 07:39 AM   #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?

Quote:
Originally Posted by Magos View Post

regards,
George
George2 is offline  
Old 04-30-2008, 02:07 AM   #7
Registered User
 
Join Date: Apr 2008
Location: USA
Posts: 22
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.
TheRaven is offline  
Old 04-30-2008, 08:08 AM   #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
George2 is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Streamwriter not writing deviousdexter C# Programming 6 11-24-2008 09:39 AM
file close issue George2 C# Programming 1 06-11-2008 12:20 AM


All times are GMT -6. The time now is 02:39 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22