<?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: IEnumerable as IEnumerable</title>
	<atom:link href="http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft/feed" rel="self" type="application/rss+xml" />
	<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=ienumerable_t_to_ienumerable_subclassoft</link>
	<description>A .NET developer in Redmond</description>
	<lastBuildDate>Tue, 31 Aug 2010 00:16:37 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Howard</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-9175</link>
		<dc:creator>Howard</dc:creator>
		<pubDate>Wed, 20 Aug 2008 16:20:15 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-9175</guid>
		<description>The code in the article isn&#039;t necessary, just use the extension method .Cast e.g. 
&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;IEnumerable&lt;T1&gt; data = [some query];
// convert to baseclass T2
IEnumberable &lt;T2&gt; converted = data.Cast&lt;T2&gt;();&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>The code in the article isn&#8217;t necessary, just use the extension method .Cast e.g. </p>
<pre><code class="csharp">IEnumerable&lt;T1&gt; data = [some query];
// convert to baseclass T2
IEnumberable &lt;T2&gt; converted = data.Cast&lt;T2&gt;();</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-9176</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Wed, 20 Aug 2008 08:26:59 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-9176</guid>
		<description>Ah yes, System.Linq now has the Cast&lt;T&gt; extension method that does something very similar but bear in mind this article was created in 2006 long before .NET 3.5 was around.

[)amien</description>
		<content:encoded><![CDATA[<p>Ah yes, System.Linq now has the Cast<t> extension method that does something very similar but bear in mind this article was created in 2006 long before .NET 3.5 was around.</p>
<p>[)amien</t></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damien Guard</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1498</link>
		<dc:creator>Damien Guard</dc:creator>
		<pubDate>Sun, 17 Jun 2007 20:29:50 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1498</guid>
		<description>Yeah that&#039;s a great solution :)&lt;br /&gt;&lt;br /&gt;[)amien</description>
		<content:encoded><![CDATA[<p>Yeah that&#8217;s a great solution :)</p>
<p>[)amien</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Judah</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1497</link>
		<dc:creator>Judah</dc:creator>
		<pubDate>Sat, 16 Jun 2007 03:14:54 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1497</guid>
		<description>I did something a little different:
&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;b&gt;public&lt;/b&gt; IEnumerable&lt;TBase&gt; As&lt;TBase, TDerived&gt;(IEnumerable&lt;TDerived&gt; items) {
   &lt;b&gt;foreach&lt;/b&gt;(TDerived derived &lt;b&gt;in&lt;/b&gt; items) &lt;b&gt;yield return&lt;/b&gt; derived;
}&lt;/code&gt;&lt;/pre&gt;
You can use it like this:
&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;IEnumerable&lt;Square&gt; squares = ...;
IEnumerable&lt;Shape&gt; shapes = As&lt;Shape, Square&gt;(squares);
&lt;/code&gt;&lt;/pre&gt;
Simple, easy, and clean. Thank you, C# yield iterators. :-)</description>
		<content:encoded><![CDATA[<p>I did something a little different:</p>
<pre><code class="csharp"><b>public</b> IEnumerable&lt;TBase&gt; As&lt;TBase, TDerived&gt;(IEnumerable&lt;TDerived&gt; items) {
   <b>foreach</b>(TDerived derived <b>in</b> items) <b>yield return</b> derived;
}</code></pre>
<p>You can use it like this:</p>
<pre><code class="csharp">IEnumerable&lt;Square&gt; squares = ...;
IEnumerable&lt;Shape&gt; shapes = As&lt;Shape, Square&gt;(squares);
</code></pre>
<p>Simple, easy, and clean. Thank you, C# yield iterators. :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1496</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sun, 28 May 2006 06:56:34 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1496</guid>
		<description>Another problem with the proposed substitutability feature is that it only makes sense in very simplistic generics cases like containers of one type. If you start using more powerful generics (I don&#039;t know if C# supports partial template specialisation like C++ does, but I know it supports generics with multiple type parametes) it gets very hairy indeed. Should you be able to pass a GroovyFunctor&lt;Base1, Derived2, SubDerived3&gt; to a LessGroovyFunctor&lt;Base1, Base2, Base3&gt;? I don&#039;t  think you can make that assertion globally, their behaviour may be completely different.&lt;br /&gt;&lt;br /&gt; IMO generic / template instances should not track their basis type&#039;s class hierarchy, it undermines their purpose as specialised adaptions of containment or external function for specific points in a class hierarchy. Personally I am completely comfortable that generics are very strongly typed such that once instantiated with a given set of types, that generic is (effectively) unique as a type, just like any other class you may define. In C++ this can never be otherwise due to the ability to mix implicit instantiation of template functions, and &lt;a href=&quot;http://ogre.cvs.sourceforge.net/ogre/ogrenew/OgreMain/include/OgreRadixSort.h?view=markup&quot;&gt;explicit special cases&lt;/a&gt;. Being able to assign implicitly between generics with different (but related) basis types would make that sort of specialisation impossible since the result would be ambiguity.&lt;br /&gt;&lt;br /&gt;That&#039;s not to say there isn&#039;t a purpose for a strongly-typed container that can be interpreted as a container of a base type instead - but I think that&#039;s not for generics to solve for you.</description>
		<content:encoded><![CDATA[<p>Another problem with the proposed substitutability feature is that it only makes sense in very simplistic generics cases like containers of one type. If you start using more powerful generics (I don&#8217;t know if C# supports partial template specialisation like C++ does, but I know it supports generics with multiple type parametes) it gets very hairy indeed. Should you be able to pass a GroovyFunctor<base1 , Derived2, SubDerived3> to a LessGroovyFunctor</base1><base1 , Base2, Base3>? I don&#8217;t  think you can make that assertion globally, their behaviour may be completely different.</p>
<p> IMO generic / template instances should not track their basis type&#8217;s class hierarchy, it undermines their purpose as specialised adaptions of containment or external function for specific points in a class hierarchy. Personally I am completely comfortable that generics are very strongly typed such that once instantiated with a given set of types, that generic is (effectively) unique as a type, just like any other class you may define. In C++ this can never be otherwise due to the ability to mix implicit instantiation of template functions, and <a href="http://ogre.cvs.sourceforge.net/ogre/ogrenew/OgreMain/include/OgreRadixSort.h?view=markup">explicit special cases</a>. Being able to assign implicitly between generics with different (but related) basis types would make that sort of specialisation impossible since the result would be ambiguity.</p>
<p>That&#8217;s not to say there isn&#8217;t a purpose for a strongly-typed container that can be interpreted as a container of a base type instead &#8211; but I think that&#8217;s not for generics to solve for you.</base1></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1495</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sun, 28 May 2006 02:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1495</guid>
		<description>&quot;you should not need to cast it to a collection of Derived&quot;&lt;br /&gt;should have read&lt;br /&gt;&quot;should not need to hold a collection specifically of Derived&quot;&lt;br /&gt;&lt;br /&gt;</description>
		<content:encoded><![CDATA[<p>&#8220;you should not need to cast it to a collection of Derived&#8221;<br />should have read<br />&#8220;should not need to hold a collection specifically of Derived&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1494</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sat, 27 May 2006 09:17:38 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1494</guid>
		<description>I know exactly what he&#039;s trying to do, my point was that the need to do something like that suggested a possible design issue.&lt;br /&gt;&lt;br /&gt;My point is that I question the need for IEnumerable&lt;Derived&gt;, period. If Derived really is-a Base, and holding a collection of Base is useful, you should not need to cast it to a collection of Derived, unless your interfaces are written in a restrictive way that requires that, at which point I question the interface design, not the language feature.&lt;br /&gt;&lt;br /&gt;I&#039;ve been using C++ with templates (generics) for 10 years with hierarchies of objects in base-type containers and have never once needed to do this kind of conversion, because polymorphism occurs on the contained types alone.</description>
		<content:encoded><![CDATA[<p>I know exactly what he&#8217;s trying to do, my point was that the need to do something like that suggested a possible design issue.</p>
<p>My point is that I question the need for IEnumerable&lt;Derived&gt;, period. If Derived really is-a Base, and holding a collection of Base is useful, you should not need to cast it to a collection of Derived, unless your interfaces are written in a restrictive way that requires that, at which point I question the interface design, not the language feature.</p>
<p>I&#8217;ve been using C++ with templates (generics) for 10 years with hierarchies of objects in base-type containers and have never once needed to do this kind of conversion, because polymorphism occurs on the contained types alone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wilco Bauwer</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1493</link>
		<dc:creator>Wilco Bauwer</dc:creator>
		<pubDate>Sat, 27 May 2006 02:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1493</guid>
		<description>The problem Damien has is that you can&#039;t pass an IEnumerable&lt;Derived&gt; to for example a method that takes an IEnumerable&lt;Base&gt;. He solves this by implementing a generic IEnumerable which returns an IEnumerator&lt;Base&gt; for a given IEnumerable&lt;Derived&gt;.&lt;br /&gt;&lt;br /&gt;With this solution he can now pass objects that implement this technique to methods that take an IEnumerable&lt;Base&gt; like this:&lt;br /&gt;&lt;br /&gt;  Foo(list.BaseClasses);&lt;br /&gt;&lt;br /&gt;It doesn&#039;t really have anything to do with casting or loops/iterators.</description>
		<content:encoded><![CDATA[<p>The problem Damien has is that you can&#8217;t pass an IEnumerable&lt;Derived&gt; to for example a method that takes an IEnumerable&lt;Base&gt;. He solves this by implementing a generic IEnumerable which returns an IEnumerator&lt;Base&gt; for a given IEnumerable&lt;Derived&gt;.</p>
<p>With this solution he can now pass objects that implement this technique to methods that take an IEnumerable&lt;Base&gt; like this:</p>
<p>  Foo(list.BaseClasses);</p>
<p>It doesn&#8217;t really have anything to do with casting or loops/iterators.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1492</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Fri, 26 May 2006 07:17:36 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1492</guid>
		<description>The visitor pattern is very popular in C++ and Java for dealing with class hierarchies in collections, but I also &lt;a href=&quot;http://www.dotnetdevs.com/articles/ReflectionVisitor.aspx&quot;&gt;found a .Net example&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Basically if generalising the collection contents is useful, then the processing abstraction given to you by the visitor pattern must also be useful - unless you&#039;ve used a base class collection for the wrong reason of course. IMO, when you hit these kinds of problems your first thought should be about the the appropriateness of the design, not a language feature.</description>
		<content:encoded><![CDATA[<p>The visitor pattern is very popular in C++ and Java for dealing with class hierarchies in collections, but I also <a href="http://www.dotnetdevs.com/articles/ReflectionVisitor.aspx">found a .Net example</a>.</p>
<p>Basically if generalising the collection contents is useful, then the processing abstraction given to you by the visitor pattern must also be useful &#8211; unless you&#8217;ve used a base class collection for the wrong reason of course. IMO, when you hit these kinds of problems your first thought should be about the the appropriateness of the design, not a language feature.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://damieng.com/blog/2006/05/24/ienumerable_t_to_ienumerable_subclassoft#comment-1491</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Fri, 26 May 2006 02:33:57 +0000</pubDate>
		<guid isPermaLink="false">http://damieng.com/blog/archive/2006/05/24/ienumerable_t_to_ienumerable_subclassoft.aspx#comment-1491</guid>
		<description>I really don&#039;t understand why this is an issue. &lt;br /&gt;&lt;br /&gt;Surely you can just use a container using instances of the base type, iterate using that, and make your polymorphism kick in for each instance of the type? We have this situation all the time with C++/STL and it&#039;s never a problem:
&lt;pre&gt;&lt;code class=&quot;cpp&quot;&gt;std::list&lt;BaseType*&gt; baseTypeList;
std::list&lt;BaseType*&gt;::iterator i  = baseTypeList.begin();

BaseType* baseType = *i;
// virtual methods on object itself
baseType-&gt;somePolymorphicMethod();
// polymorphic non-member functions
somePolymorphicFunction(baseType);
&lt;/code&gt;&lt;/pre&gt;
You should never need to cast the container itself, all that matters is maintaining polymorphism on the elements it contains. I think the fact that C# makes / encourages you define a subtype rather than thinking of a generic usage like a typedef (type alias) makes you think you have to make your methods polymorphic on the container, which you do not.</description>
		<content:encoded><![CDATA[<p>I really don&#8217;t understand why this is an issue. </p>
<p>Surely you can just use a container using instances of the base type, iterate using that, and make your polymorphism kick in for each instance of the type? We have this situation all the time with C++/STL and it&#8217;s never a problem:</p>
<pre><code class="cpp">std::list&lt;BaseType*&gt; baseTypeList;
std::list&lt;BaseType*&gt;::iterator i  = baseTypeList.begin();

BaseType* baseType = *i;
// virtual methods on object itself
baseType-&gt;somePolymorphicMethod();
// polymorphic non-member functions
somePolymorphicFunction(baseType);
</code></pre>
<p>You should never need to cast the container itself, all that matters is maintaining polymorphism on the elements it contains. I think the fact that C# makes / encourages you define a subtype rather than thinking of a generic usage like a typedef (type alias) makes you think you have to make your methods polymorphic on the container, which you do not.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
