<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>From The Depths : .NET</title><link>http://blogs.msdn.com/mthalman/archive/tags/.NET/default.aspx</link><description>Tags: .NET</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Should Interfaces Derive from IDisposable?</title><link>http://blogs.msdn.com/mthalman/archive/2009/03/13/should-interfaces-derive-from-idisposable.aspx</link><pubDate>Fri, 13 Mar 2009 20:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9473424</guid><dc:creator>mthalman</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/mthalman/comments/9473424.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mthalman/commentrss.aspx?PostID=9473424</wfw:commentRss><description>&lt;P&gt;I've been defining some interfaces for some classes in our code.&amp;nbsp; The classes implement IDisposable and I was wondering whether the class' corresponding interface should also derive from IDisposable.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;class Foo : IFoo, IDisposable&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Bar() { }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Dispose() { }&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;interface IFoo&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void Bar();&lt;BR&gt;}&lt;BR&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The question is: should IFoo derive from IDisposable?&amp;nbsp; My co-worker and I both decided that it should not.&amp;nbsp; Our reasoning was that the dispose implementation should only exist if the class required it.&amp;nbsp; Since the interface can have varying implementations, it is up to the class to define whether it implements IDisposable.&amp;nbsp; The IDisposable interface doesn't really contribute to contract of the interface; it's purely an implementation detail that should not be exposed at that level.&lt;/P&gt;
&lt;P&gt;Unfortunately, this requires that consuming code check whether the object implements IDisposable if they want to dispose of it:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;IFoo foo = ...;&lt;BR&gt;IDisposable disposableFoo = foo as IDisposable;&lt;BR&gt;if (disposableFoo != null)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; disposableFoo.Dispose();&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What are your thoughts?&amp;nbsp; Do you agree?&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9473424" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mthalman/archive/tags/C_2300_+Code/default.aspx">C# Code</category><category domain="http://blogs.msdn.com/mthalman/archive/tags/IDisposable/default.aspx">IDisposable</category><category domain="http://blogs.msdn.com/mthalman/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>