<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Don&#8217;t forget to close StreamWriter</title>
	<atom:link href="http://www.developingfor.net/net/dont-forget-to-close-streamwriter.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.developingfor.net/net/dont-forget-to-close-streamwriter.html</link>
	<description>Real World .NET Methods, Tricks, and Examples</description>
	<lastBuildDate>Sat, 31 Jul 2010 00:54:16 -0400</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Joel</title>
		<link>http://www.developingfor.net/net/dont-forget-to-close-streamwriter.html/comment-page-1#comment-20</link>
		<dc:creator>Joel</dc:creator>
		<pubDate>Tue, 25 Dec 2007 02:07:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/miscellaneous/dont-forget-to-close-streamwriter.html#comment-20</guid>
		<description>Sure, I see your point: I didn&#039;t really consider the Try-Catch block because in and of itself it does not address the problem I was addressing.  I do see the difference in what you are saying, and I think you are correct that using is probably a better approach: ultimately though, the developer needs to ensure that the FileStream is properly closed.  Try-Catch does not solve that problem.</description>
		<content:encoded><![CDATA[<p>Sure, I see your point: I didn&#8217;t really consider the Try-Catch block because in and of itself it does not address the problem I was addressing.  I do see the difference in what you are saying, and I think you are correct that using is probably a better approach: ultimately though, the developer needs to ensure that the FileStream is properly closed.  Try-Catch does not solve that problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmytro Shteflyuk</title>
		<link>http://www.developingfor.net/net/dont-forget-to-close-streamwriter.html/comment-page-1#comment-15</link>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
		<pubDate>Sun, 23 Dec 2007 11:25:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/miscellaneous/dont-forget-to-close-streamwriter.html#comment-15</guid>
		<description>Hi Joel,

First of all you should read manual. Why? Because you are not see differences between destructors and IDispose. In two words: all you said is about desctructor (objects being destroyed when GC wants), but if you are using &lt;em&gt;using&lt;/em&gt; keyword, object will be destroyed just right after the block finished.

Why your approach with &lt;em&gt;Close&lt;/em&gt; method calling is wrong? Tell me, what will happen if &lt;em&gt;WriteLine&lt;/em&gt; method will throw an exception? Right, you stream will not be closed. But in case of using keyword stream will be closed even if exception thrown. Do you see difference?

Keep your eyes opened.</description>
		<content:encoded><![CDATA[<p>Hi Joel,</p>
<p>First of all you should read manual. Why? Because you are not see differences between destructors and IDispose. In two words: all you said is about desctructor (objects being destroyed when GC wants), but if you are using <em>using</em> keyword, object will be destroyed just right after the block finished.</p>
<p>Why your approach with <em>Close</em> method calling is wrong? Tell me, what will happen if <em>WriteLine</em> method will throw an exception? Right, you stream will not be closed. But in case of using keyword stream will be closed even if exception thrown. Do you see difference?</p>
<p>Keep your eyes opened.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joel</title>
		<link>http://www.developingfor.net/net/dont-forget-to-close-streamwriter.html/comment-page-1#comment-14</link>
		<dc:creator>Joel</dc:creator>
		<pubDate>Sun, 23 Dec 2007 03:29:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/miscellaneous/dont-forget-to-close-streamwriter.html#comment-14</guid>
		<description>Hi Dmytro,

What using buys you is that the StreamWriter object gets marked for disposal, which I now have to assume means that Close is called at the appropriate time.  I agree that &quot;using&quot; is a good approach for many things, but since the Close is the last thing I do in this program I essentially get the same result.  In this case, though, since I&#039;m explicitly controlling the Close, I know that the file will be completed immediately.  With using, assuming that it will properly complete the file, I have to wait until the GC decides to reclaim the object.

I could wrap the code in a Try-Catch-Finally set, but that would not address the problem I encountered, so I would still want to issue the Close() statement in the finally block.</description>
		<content:encoded><![CDATA[<p>Hi Dmytro,</p>
<p>What using buys you is that the StreamWriter object gets marked for disposal, which I now have to assume means that Close is called at the appropriate time.  I agree that &#8220;using&#8221; is a good approach for many things, but since the Close is the last thing I do in this program I essentially get the same result.  In this case, though, since I&#8217;m explicitly controlling the Close, I know that the file will be completed immediately.  With using, assuming that it will properly complete the file, I have to wait until the GC decides to reclaim the object.</p>
<p>I could wrap the code in a Try-Catch-Finally set, but that would not address the problem I encountered, so I would still want to issue the Close() statement in the finally block.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmytro Shteflyuk</title>
		<link>http://www.developingfor.net/net/dont-forget-to-close-streamwriter.html/comment-page-1#comment-13</link>
		<dc:creator>Dmytro Shteflyuk</dc:creator>
		<pubDate>Fri, 21 Dec 2007 20:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.developingfor.net/miscellaneous/dont-forget-to-close-streamwriter.html#comment-13</guid>
		<description>Hey, don&#039;t do it!
You should use &lt;em&gt;using&lt;/em&gt; keyword or try/finally block here.


using(StreamWriter file = File.CreateText(logFileName))
{
    foreach (var item in collection)
    {
        file.WriteLine(&quot;Some Text&quot;);
    }
}
</description>
		<content:encoded><![CDATA[<p>Hey, don&#8217;t do it!<br />
You should use <em>using</em> keyword or try/finally block here.</p>
<p>using(StreamWriter file = File.CreateText(logFileName))<br />
{<br />
    foreach (var item in collection)<br />
    {<br />
        file.WriteLine(&#8220;Some Text&#8221;);<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
