<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>the blog =&gt; anything goes : polymorphism</title><link>http://msmvps.com/blogs/senthil/archive/tags/polymorphism/default.aspx</link><description>Tags: polymorphism</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Mixing generics and polymorphism</title><link>http://msmvps.com/blogs/senthil/archive/2007/10/14/mixing-generics-and-polymorphism.aspx</link><pubDate>Sun, 14 Oct 2007 16:29:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1247436</guid><dc:creator>Senthil</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/senthil/rsscomments.aspx?PostID=1247436</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/senthil/commentapi.aspx?PostID=1247436</wfw:comment><comments>http://msmvps.com/blogs/senthil/archive/2007/10/14/mixing-generics-and-polymorphism.aspx#comments</comments><description>&lt;p&gt;Polymorphism, which attempts to hide differences in implementation, and generics, which attemtps to highlight them by providing exact information about types, don&amp;#39;t seem to mix very well. Consider the following fairly common pattern.&lt;/p&gt;&lt;pre&gt;        class Base
        {
        }

        class Derived : Base
        {
        }

        abstract class Manipulator
        {
            List&amp;lt;Base&amp;gt; list;

            public Manipulator(List&amp;lt;Base&amp;gt; list)
            {
                this.list = list;
            }
         }

        class BaseManipulator : Manipulator
        {
            public BaseManipulator(List&amp;lt;Base&amp;gt; list)
                : base(list)
            { }
        }

        class DerivedManipulator : Manipulator
        {
            public DerivedManipulator(List&amp;lt;Derived&amp;gt; list)
                : base(list)
            { }
        }

        public static void Main()
        {
            List&amp;lt;Derived&amp;gt; list = new List&amp;lt;Derived&amp;gt;();
            list.Add(new Derived());
            DerivedManipulator d = new DerivedManipulator(list);
        }
&lt;/pre&gt;
&lt;p&gt;The code above will not compile - the compiler complains that List&amp;lt;Derived&amp;gt;&amp;nbsp;cannot be converted to List&amp;lt;Base&amp;gt; in DerivedManipulator&amp;#39;s constructor. Which seems kindof strange, given that Derived[] is implicitly convertible to Base[]. But what would happen if implicit conversion occurred with generics?&lt;/p&gt;&lt;pre&gt;List&amp;lt;Derived&amp;gt; derivedList = new List&amp;lt;Derived&amp;gt;();
List&amp;lt;Base&amp;gt; baseList = derivedList;
baseList.Add(new Base());
Derived d = derivedList[0]; // BOOM&lt;/pre&gt;
&lt;p&gt;All hell&amp;nbsp;will break loose. The type safety&amp;nbsp;that generics offer will disappear, obviating the very need for generics.&lt;/p&gt;
&lt;p&gt;What can be done then? We could make Manipulator a generic class, taking the type of the List as a generic parameter and make DerivedManipulator derive from Manipulator&amp;lt;Derived&amp;gt;. But then we are only shifting the problem - treating Manipulators polymorphically becomes impossible then. &lt;/p&gt;
&lt;p&gt;There seems to be a basic impedance mismatch between polymorphism and generics. Does anyone know of a better way to solve the problem?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1247436" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/senthil/archive/tags/software/default.aspx">software</category><category domain="http://msmvps.com/blogs/senthil/archive/tags/software+C_2300_+language/default.aspx">software C# language</category><category domain="http://msmvps.com/blogs/senthil/archive/tags/generics/default.aspx">generics</category><category domain="http://msmvps.com/blogs/senthil/archive/tags/polymorphism/default.aspx">polymorphism</category></item></channel></rss>