<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: IDisposable &#8211; 1, 2, 3, &#8230;</title>
	<atom:link href="http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/</link>
	<description>Thoughts and informations I think worthwhile to share...</description>
	<lastBuildDate>Fri, 18 Sep 2009 01:42:12 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: ajdotnet</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7973</link>
		<dc:creator>ajdotnet</dc:creator>
		<pubDate>Tue, 22 Jan 2008 06:05:38 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7973</guid>
		<description>I read you initial use case in #11 as if the list class itself implemented IDisposable, but if that is not the case your code in #15 is essentially correct.

Once you have to do that more often, you may welcome a set of (simple enough) helper methods:

[DebuggerStepThrough]
public static void Dispose(IDisposable id)
{
        if (id != null)
        {
                id.Dispose();
        }
}
 
[DebuggerStepThrough]
public static void Dispose(object o)
{
        IDisposable disposable = o as IDisposable;
        if (disposable != null)
        {
                disposable.Dispose();
        }
}

public static void Dispose&lt;T&gt;(ref T o)
{
        IDisposable disposable = o as IDisposable;
        if (disposable != null)
        {
                disposable.Dispose();
        }
        o = default(T);
}

[DebuggerStepThrough]
public static void DisposeElements(IEnumerable enumerable)
{
        if (enumerable != null)
        {
                IEnumerator enumerator = enumerable.GetEnumerator();
                while (enumerator.MoveNext())
                {
                        Dispose(enumerator.Current);
                }
        }
}

With this your Dispose(bool) method would look like this:

protected virtual void Dispose(bool disposing)
{
        if (disposing)
               MyHelper.DisposeElements(_ListofB);
}

HIH, AJ.NET</description>
		<content:encoded><![CDATA[<p>I read you initial use case in #11 as if the list class itself implemented IDisposable, but if that is not the case your code in #15 is essentially correct.</p>
<p>Once you have to do that more often, you may welcome a set of (simple enough) helper methods:</p>
<p>[DebuggerStepThrough]<br />
public static void Dispose(IDisposable id)<br />
{<br />
        if (id != null)<br />
        {<br />
                id.Dispose();<br />
        }<br />
}</p>
<p>[DebuggerStepThrough]<br />
public static void Dispose(object o)<br />
{<br />
        IDisposable disposable = o as IDisposable;<br />
        if (disposable != null)<br />
        {<br />
                disposable.Dispose();<br />
        }<br />
}</p>
<p>public static void Dispose&lt;T&gt;(ref T o)<br />
{<br />
        IDisposable disposable = o as IDisposable;<br />
        if (disposable != null)<br />
        {<br />
                disposable.Dispose();<br />
        }<br />
        o = default(T);<br />
}</p>
<p>[DebuggerStepThrough]<br />
public static void DisposeElements(IEnumerable enumerable)<br />
{<br />
        if (enumerable != null)<br />
        {<br />
                IEnumerator enumerator = enumerable.GetEnumerator();<br />
                while (enumerator.MoveNext())<br />
                {<br />
                        Dispose(enumerator.Current);<br />
                }<br />
        }<br />
}</p>
<p>With this your Dispose(bool) method would look like this:</p>
<p>protected virtual void Dispose(bool disposing)<br />
{<br />
        if (disposing)<br />
               MyHelper.DisposeElements(_ListofB);<br />
}</p>
<p>HIH, AJ.NET</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mohan</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7972</link>
		<dc:creator>Mohan</dc:creator>
		<pubDate>Mon, 21 Jan 2008 20:21:56 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7972</guid>
		<description>Also, as per comment 12: should I simple iterate thru the collection in A class Dispose(bool) method calling b.dispose - here what i mean.

class A:IDisposable
{
private List _ListofB;

        public void Dispose()
        {
            Dispose(true);
            System.GC.SuppressFinalize(this);
        }

       protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                foreach (CartItem c in CartItemList)
                {
                    c.Dispose();
                }
                

            }
        }
 
}

Sorry abt comment 14. I pressed Submit Comment inadvertently, and thanks for the help</description>
		<content:encoded><![CDATA[<p>Also, as per comment 12: should I simple iterate thru the collection in A class Dispose(bool) method calling b.dispose &#8211; here what i mean.</p>
<p>class A:IDisposable<br />
{<br />
private List _ListofB;</p>
<p>        public void Dispose()<br />
        {<br />
            Dispose(true);<br />
            System.GC.SuppressFinalize(this);<br />
        }</p>
<p>       protected virtual void Dispose(bool disposing)<br />
        {<br />
            if (disposing)<br />
            {<br />
                foreach (CartItem c in CartItemList)<br />
                {<br />
                    c.Dispose();<br />
                }</p>
<p>            }<br />
        }</p>
<p>}</p>
<p>Sorry abt comment 14. I pressed Submit Comment inadvertently, and thanks for the help</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mohan</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7971</link>
		<dc:creator>Mohan</dc:creator>
		<pubDate>Mon, 21 Jan 2008 20:18:54 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7971</guid>
		<description>Also, as per comment 12: should I simple iterate thru the collection in A class Dispose(bool) method calling b.dispose - here what i mean.

class A
{
   private List&lt;B&gt; _ListofB;


}</description>
		<content:encoded><![CDATA[<p>Also, as per comment 12: should I simple iterate thru the collection in A class Dispose(bool) method calling b.dispose &#8211; here what i mean.</p>
<p>class A<br />
{<br />
   private List<b> _ListofB;</p>
<p>}</b></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mohan</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7967</link>
		<dc:creator>Mohan</dc:creator>
		<pubDate>Sat, 19 Jan 2008 22:08:31 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7967</guid>
		<description>AJ.NET: I will go ahead and make the change in my code, however, would there have been any negative performance impact between the way I earlier implemented the IDisposable interface vs. not implementing the IDisposable interface at all. thanks, Mohan</description>
		<content:encoded><![CDATA[<p>AJ.NET: I will go ahead and make the change in my code, however, would there have been any negative performance impact between the way I earlier implemented the IDisposable interface vs. not implementing the IDisposable interface at all. thanks, Mohan</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ajdotnet</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7966</link>
		<dc:creator>ajdotnet</dc:creator>
		<pubDate>Sat, 19 Jan 2008 20:29:33 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7966</guid>
		<description>Mohan, your class A should actually call B.Dispose() in its own Dispose(bool) method. Apart from that clarification this looks perfectly well.
AJ.NET</description>
		<content:encoded><![CDATA[<p>Mohan, your class A should actually call B.Dispose() in its own Dispose(bool) method. Apart from that clarification this looks perfectly well.<br />
AJ.NET</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mohan</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7964</link>
		<dc:creator>Mohan</dc:creator>
		<pubDate>Sat, 19 Jan 2008 02:12:37 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-7964</guid>
		<description>here my situation - I have a class - let&#039;s call it A. This class implements IDisposable and within the dispose method I have implemented gc.suppressfinalize(this). Class A holds reference to collection of Objects. This collection is essentially class B. Again, I have implemented IDisposable Interface and within the dispose method I have implemented gc.suppressfinalize(this). Is this the right way of implementing things or am i missing something?</description>
		<content:encoded><![CDATA[<p>here my situation &#8211; I have a class &#8211; let&#8217;s call it A. This class implements IDisposable and within the dispose method I have implemented gc.suppressfinalize(this). Class A holds reference to collection of Objects. This collection is essentially class B. Again, I have implemented IDisposable Interface and within the dispose method I have implemented gc.suppressfinalize(this). Is this the right way of implementing things or am i missing something?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AdFactum ObjectMapper .NET Blog &#187; Blog Archive &#187; New Release - AdFactum ObjectMapper .NET 1.90.1917.0</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-5229</link>
		<dc:creator>AdFactum ObjectMapper .NET Blog &#187; Blog Archive &#187; New Release - AdFactum ObjectMapper .NET 1.90.1917.0</dc:creator>
		<pubDate>Mon, 17 Sep 2007 19:20:12 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-5229</guid>
		<description>[...] As&#160;third improvement I re-worked all classes that implements the IDisposable interface in order to use the correct IDisposable Pattern. [...]</description>
		<content:encoded><![CDATA[<p>[...] As&nbsp;third improvement I re-worked all classes that implements the IDisposable interface in order to use the correct IDisposable Pattern. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IDisposable - 1,2,3 &#8230; &#171; All about nothing</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-4724</link>
		<dc:creator>IDisposable - 1,2,3 &#8230; &#171; All about nothing</dc:creator>
		<pubDate>Thu, 23 Aug 2007 13:07:09 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-4724</guid>
		<description>[...] http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/  [...]</description>
		<content:encoded><![CDATA[<p>[...] <a href="http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/" rel="nofollow">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/</a>  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ajdotnet</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-3653</link>
		<dc:creator>ajdotnet</dc:creator>
		<pubDate>Mon, 02 Jul 2007 06:44:26 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-3653</guid>
		<description>@Egil: I asume that C is still around when you dispose B (otherwise you won&#039;t have to deal with deregistering the event at all). And yes, you should call GC.SuppressFinalize(this) in Dispose(). One reason is simply &quot;the pattern says so&quot; :-). It&#039;s part of the beauty of patterns that you don&#039;t have to think about these things anymore, just do it and go on the important stuff. The technical reason is simply the the next GC will collect B if you do; if you didn&#039;t call the method, B would linger around for another GC, making it to the next generation.

Regarding the other resources: They are not set to null automatically but you don&#039;t have to do that manually either. Since they are not reachable by a living object, the GC will collect them anyway. Personally I set members to null only after I explicitly disposed them (just as a safety measure). Anyway, the call to GC.SuppressFinalize(this) will not affect any of those objects, i.e. one had a finalizer, it would still be called.

You could of course refrain from using the pattern at all; a simple Unsubscribe() method called instead of Dispose() might be sufficient in your case. In this case you don&#039;t have to bother with a finalizer and GC.SuppressFinalize(this) at all. After all, the primary purpose for finalizers is dealing with unmanaged resources (if only referenced indirectly).</description>
		<content:encoded><![CDATA[<p>@Egil: I asume that C is still around when you dispose B (otherwise you won&#8217;t have to deal with deregistering the event at all). And yes, you should call GC.SuppressFinalize(this) in Dispose(). One reason is simply &#8220;the pattern says so&#8221; <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . It&#8217;s part of the beauty of patterns that you don&#8217;t have to think about these things anymore, just do it and go on the important stuff. The technical reason is simply the the next GC will collect B if you do; if you didn&#8217;t call the method, B would linger around for another GC, making it to the next generation.</p>
<p>Regarding the other resources: They are not set to null automatically but you don&#8217;t have to do that manually either. Since they are not reachable by a living object, the GC will collect them anyway. Personally I set members to null only after I explicitly disposed them (just as a safety measure). Anyway, the call to GC.SuppressFinalize(this) will not affect any of those objects, i.e. one had a finalizer, it would still be called.</p>
<p>You could of course refrain from using the pattern at all; a simple Unsubscribe() method called instead of Dispose() might be sufficient in your case. In this case you don&#8217;t have to bother with a finalizer and GC.SuppressFinalize(this) at all. After all, the primary purpose for finalizers is dealing with unmanaged resources (if only referenced indirectly).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Egil Hansen</title>
		<link>http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-3645</link>
		<dc:creator>Egil Hansen</dc:creator>
		<pubDate>Sun, 01 Jul 2007 16:44:56 +0000</pubDate>
		<guid isPermaLink="false">http://ajdotnet.wordpress.com/2007/05/28/idisposable-1-2-3/#comment-3645</guid>
		<description>Hi there

Great article. I want your input on this particular case I have.

I have a class (A) that holds a reference to another class (B). Class B then subscribes to an event published by another class (C). At some point, class A might not need class B any more, and class B should be GC&#039;ed. However, since class B has subscribed to class C event, it&#039;s not GC&#039;ed.

So, if I write a basic implementation of the IDisposable pattern (like you propose in your blog post here), class A would simply call class B&#039;s dispose method when it is no longer needs class B. The dispose method in class B would then unsubscribe to class C&#039;s event, and then call GC.SuppressFinalize(this).

But, should I call GC.SuppressFinalize(this)?

Class B has a few other managed resources besides the event hook, managed resources like List objects and other objects that class B uses. Are those automatically set to null or do I also have to set those to null manually if I call GC.SuppressFinalize(this)?

Would it, in that case, be better just to unsubscribe to class C&#039;s event in the Displose method and then NOT call GC.SuppressFinalize(this) - having the  GC do it&#039;s magic as it would do normally.</description>
		<content:encoded><![CDATA[<p>Hi there</p>
<p>Great article. I want your input on this particular case I have.</p>
<p>I have a class (A) that holds a reference to another class (B). Class B then subscribes to an event published by another class (C). At some point, class A might not need class B any more, and class B should be GC&#8217;ed. However, since class B has subscribed to class C event, it&#8217;s not GC&#8217;ed.</p>
<p>So, if I write a basic implementation of the IDisposable pattern (like you propose in your blog post here), class A would simply call class B&#8217;s dispose method when it is no longer needs class B. The dispose method in class B would then unsubscribe to class C&#8217;s event, and then call GC.SuppressFinalize(this).</p>
<p>But, should I call GC.SuppressFinalize(this)?</p>
<p>Class B has a few other managed resources besides the event hook, managed resources like List objects and other objects that class B uses. Are those automatically set to null or do I also have to set those to null manually if I call GC.SuppressFinalize(this)?</p>
<p>Would it, in that case, be better just to unsubscribe to class C&#8217;s event in the Displose method and then NOT call GC.SuppressFinalize(this) &#8211; having the  GC do it&#8217;s magic as it would do normally.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
