![]() |
| | #1 |
| Registered User Join Date: May 2006
Posts: 1,579
| StreamWriter 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 |
| | #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 |
| | #3 |
| and the Hat of Clumsiness 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 |
| | #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:
regards, George | |
| George2 is offline |
| | #5 | |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| Quote:
__________________ 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 |
| | #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: regards, George | |
| George2 is offline |
| | #7 |
| Registered User Join Date: Apr 2008 Location: USA
Posts: 22
| 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 |
| | #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:
regards, George | |
| George2 is offline |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |