[Mono-list] Buffering for StreamWriter

Sanin Saracevic ssaracevic@interland.com
Thu, 16 May 2002 13:58:29 -0400

Not really sure about other languages, but I would say no. However, you can
certainly implement it yourself since using is nothing more than a
try-catch-finally construct with a call to Dispose() inside the finally

Sanin Saracevic, MCP 
Lead Software Architect 
Interland, Inc. 
W: 404.260.2477x4562

-----Original Message-----
From: Chris J. Breisch [mailto:cjbreisch@altavista.net]
Sent: Thursday, May 16, 2002 1:31 PM
To: 'Miguel de Icaza'; 'Jerry Houston'
Cc: 'Nick Drochak'; 'Mono-List'
Subject: RE: [Mono-list] Buffering for StreamWriter

Wow...you learn something new every day.  Didn't know about this
construct.  Is there a similar construct in the other .NET languages?


Chris J. Breisch, MCSD, MCDBA

-----Original Message-----
From: mono-list-admin@ximian.com [mailto:mono-list-admin@ximian.com] On
Behalf Of Miguel de Icaza
Sent: Thursday, May 16, 2002 11:20 AM
To: Jerry Houston
Cc: Nick Drochak; Mono-List
Subject: Re: [Mono-list] Buffering for StreamWriter

> I always call Flush() on StreamWriter before calling Close().  Is that
> unnecessary?  And if you Flush() but don't Close(), is the file
> closed anyway when the StreamWriter goes out of scope?

My guess is that the file will not be closed when the StreamWriter goes
out of scope (unless we add ourselves a destructor, but even then, I am
not sure if the GC system will guarantee that the destructor will be

For classes that implement IDisposable (like StreamWriter), you have to
call the Dispose method (or you can manually call close in this case) to
make sure that their resources are released (in this case an OS file
handle is the precious resource).

You can use the `using' statement in C# to do this for you:

	using (f = new StreamWriter (...)){

That will make sure that the file is properly disposed/flushed (if you
return, if there is an exception, or if you just leave the block; the
file will be released).


Mono-list maillist  -  Mono-list@ximian.com

Mono-list maillist  -  Mono-list@ximian.com