<?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>Krzysztof Cwalina : System.Collections</title><link>http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx</link><description>Tags: System.Collections</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Simulated Covariance for .NET Generics</title><link>http://blogs.msdn.com/kcwalina/archive/2008/04/02/SimulatedCovariance.aspx</link><pubDate>Wed, 02 Apr 2008 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8348605</guid><dc:creator>kcwalina</dc:creator><slash:comments>18</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/8348605.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=8348605</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=8348605</wfw:comment><description>&lt;H2 style="MARGIN: 12pt 0in 6pt 0.4in; mso-list: none"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#943634 size=3&gt;I&amp;nbsp;just&amp;nbsp;wrote this pattern,&amp;nbsp;but I am not sure if I should add it&amp;nbsp;officially to the Framework Design Guidelines. It seems like a bit of a corner case scenario, though I do get questions about it from time to time. Anyway, let me know what you think.&lt;/FONT&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/H2&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;Different constructed types don’t have a common root type. For example, there would not be a common representation of IEnumerable&amp;lt;string&amp;gt; and IEnumerable&amp;lt;object&amp;gt; if not for a pattern implemented by IEnumerable&amp;lt;T&amp;gt; called &lt;I style="mso-bidi-font-style: normal"&gt;Simulated Covariance&lt;/I&gt;. This post describes the details of the pattern.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;Generics is a very powerful type system feature added to the .NET Framework 2.0. It allows creation of so called parameterized types. For example, List&amp;lt;T&amp;gt; is such a type and it represents a list of objects of type T. The T is specified at the time when the instance of the list is created. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;List&amp;lt;string&amp;gt; names = new List&amp;lt;string&amp;gt;();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;names.Add(“John Smith”);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;names.Add(“Mary Johnson”);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;Such Generic data structures have many benefits over their non-Generic counterparts. But they also have some, sometimes surprising, limitations. For example, some users expect that a List&amp;lt;string&amp;gt; can be cast to List&amp;lt;object&amp;gt;, just as a String can be cast to Object. But unfortunately, the following code won’t even compile.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;List&amp;lt;string&amp;gt; names = new List&amp;lt;string&amp;gt;();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;List&amp;lt;object&amp;gt; objects = names; // this won’t compile&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;There is a very good reason for this limitation, and that is to allow for full strong typing. For example, if you could cast List&amp;lt;string&amp;gt; to a List&amp;lt;object&amp;gt; the following incorrect code would compile, but the program would fail at runtime.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;static void Main(){&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;List&amp;lt;string&amp;gt; names = new List&amp;lt;string&amp;gt;();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// this of course does not compile, but if it did&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// the whole program would compile, but would be incorrect as it &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// attempts to add arbitrary objects to a list of strings.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;AddObjects((List&amp;lt;object&amp;gt;)names); &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;string name = names[0]; // how could this work? &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// this would (and does) compile just fine.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;static void AddObjects(List&amp;lt;object&amp;gt; list){&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;list.Add(new object()); // it’s a list of strings, really. Should we throw?&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;list.Add(new Button());&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;} &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;Unfortunately this limitation can also be undesired in some scenarios. For example, there is nothing wrong with casting a List&amp;lt;string&amp;gt; to IEnumerable&amp;lt;object&amp;gt;, like in the following example.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;List&amp;lt;string&amp;gt; names = new List&amp;lt;string&amp;gt;();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;IEnumerable&amp;lt;object&amp;gt; objects = names; // this won’t compile&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;foreach(object obj in objects){&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console.WriteLine(obj.ToString());&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;In general, having a way to represent “any list” (or in general “any instance of this generic type”) is very useful. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// what type should ??? be?&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;static void PrintItems(??? anyList){&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;foreach(object obj in anyList){&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console.WriteLine(obj.ToString());&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;Unfortunately, unless List&amp;lt;T&amp;gt; implemented a pattern that will be described in a moment, the only common representation of all List&amp;lt;T&amp;gt; instances would be System.Object. But System.Object is too limiting and would not allow PrintItems method to enumerate items in the list.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;The reason that casting to IEnumerable&amp;lt;object&amp;gt; is just fine, but casting to List&amp;lt;object&amp;gt; can cause all sorts of problems is that in case of IEnumerable&amp;lt;object&amp;gt;, the object appears only in the output position (the return type of GetEnumerator is IEnumerator&amp;lt;object&amp;gt;). In case of List&amp;lt;object&amp;gt;, the object represents both output and input types. For example, object is the type of the input to the Add method. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// T does not appear as input to any members or dependencies of this interface&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public interface IEnumerable&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;IEnumerator&amp;lt;T&amp;gt; GetEnumerator(); &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public interface IEnumerator&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;T Current { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;bool MoveNext();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;} &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;// T does appear as input to members of List&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class List&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public void Add(T item); // T is an input here&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T this[int index]{&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;get; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;set; // T is actually an input here&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;In other words, we say that in IEnumerable&amp;lt;T&amp;gt;, the T is at covariant positions (outputs). In List&amp;lt;T&amp;gt;, the T is at covariant and contravariant (inputs) positions. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;To solve the problem of not having a common type representing the root of all constructions of a generic type, you can implement what’s called the &lt;I style="mso-bidi-font-style: normal"&gt;Simulated Covariance Pattern&lt;/I&gt;.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;Given a generic type (class or interface) and its dependencies&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-FAMILY: 'Verdana','sans-serif'"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class Foo&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property1 { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property2 { set; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property3 { get; set; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public void Method1(T arg1);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public T Method2();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Method3(T arg);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public Type1&amp;lt;T&amp;gt; GetMethod1(); &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public Type2&amp;lt;T&amp;gt; GetMethod2();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class Type1&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class Type2&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property { get; set; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;Create a new interface (&lt;I style="mso-bidi-font-style: normal"&gt;Root Type&lt;/I&gt;) with all members containing Ts at contravariant positions removed. In addition, feel free to remove all members that might not make sense in the context of the trimmed down type.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-FAMILY: 'Verdana','sans-serif'"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public interface IFoo&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;T Property1 { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;T Property3 { get; } // setter removed&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;T Method2();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Type1&amp;lt;T&amp;gt; GetMethod1();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;IType2&amp;lt;T&amp;gt; GetMethod2(); // note that the return type changed&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public interface IType2&amp;lt;T&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;T Property { get; } // setter removed&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;The generic type should then implement the interface explicitly and “add back” the strongly typed members (using T instead of object) to its public API surface.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-FAMILY: 'Verdana','sans-serif'"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class Foo&amp;lt;T&amp;gt; : IFoo&amp;lt;object&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property1 { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property2 { set; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property3 { get; set;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public void Method1(T arg1);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Method2();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Method3(T arg);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public Type1&amp;lt;T&amp;gt; GetMethod1();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public Type2&amp;lt;T&amp;gt; GetMethod2();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;object IFoo&amp;lt;object&amp;gt;.Property1 { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;object IFoo&amp;lt;object&amp;gt;.Property3 { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;object IFoo&amp;lt;object&amp;gt;.Method2() { return null; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Type1&amp;lt;object&amp;gt; IFoo&amp;lt;object&amp;gt;.GetMethod1();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;IType2&amp;lt;object&amp;gt; IFoo&amp;lt;object&amp;gt;.GetMethod2();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class Type2&amp;lt;T&amp;gt; : IType2&amp;lt;object&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property { get; set; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;object IType2&amp;lt;object&amp;gt;.Property { get; }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Verdana','sans-serif'"&gt;Now, all constructed instantiation of Foo&amp;lt;T&amp;gt; have a common root type IFoo&amp;lt;object&amp;gt;.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-FAMILY: 'Verdana','sans-serif'"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;var foos = new List&amp;lt;IFoo&amp;lt;object&amp;gt;&amp;gt;();&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;foos.Add(new Foo&amp;lt;int&amp;gt;());&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;foos.Add(new Foo&amp;lt;string&amp;gt;());&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;…&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;foreach(IFoo&amp;lt;object&amp;gt; foo in foos){&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console.WriteLine(foo.Property1);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Console.WriteLine(foo.GetMethod2().Property);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in 3pt 12.25pt; TEXT-INDENT: -12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN class=checkbox&gt;&lt;SPAN style="FONT-SIZE: 11pt; COLOR: #00b050; FONT-FAMILY: Wingdings; mso-bidi-font-family: ZapfDingbats; mso-ascii-font-family: ZapfDingbats; mso-hansi-font-family: ZapfDingbats; mso-char-type: symbol; mso-symbol-font-family: Wingdings; mso-bidi-font-size: 16.0pt"&gt;&lt;SPAN style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;þ&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 11pt"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN class=DO1&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;&lt;SPAN style="FONT-SIZE: 11pt; mso-bidi-font-family: MetaBlack-Roman"&gt;CONSIDER&lt;/SPAN&gt;&lt;/U&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt; using the &lt;I style="mso-bidi-font-style: normal"&gt;Simulated Covariance Pattern&lt;/I&gt;, if there is a need to have a representation for all instantiations of a generic type&lt;/SPAN&gt;.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 0in 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;The pattern should not be used frivolously as it results in additional types in the library and can makes the existing types more complex.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 0in 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face=Verdana&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in 3pt 12.25pt; TEXT-INDENT: -12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN class=checkbox&gt;&lt;SPAN style="FONT-SIZE: 11pt; COLOR: #00b050; FONT-FAMILY: Wingdings; mso-bidi-font-family: ZapfDingbats; mso-ascii-font-family: ZapfDingbats; mso-hansi-font-family: ZapfDingbats; mso-char-type: symbol; mso-symbol-font-family: Wingdings; mso-bidi-font-size: 16.0pt"&gt;&lt;SPAN style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;þ&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 11pt"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN class=DO1&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;&lt;SPAN style="FONT-SIZE: 11pt; mso-bidi-font-family: MetaBlack-Roman"&gt;DO&lt;/SPAN&gt;&lt;/U&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt; ensure that the implementation of the root’s members is equivalent to the implementation of the corresponding generic type members. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 0in 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt;&lt;FONT face=Verdana&gt;There should not be an observable difference between calling a member on the root type and calling the corresponding member on the generic type. In many cases the members of the root are implemented by calling members on the generic type.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;public class Foo&amp;lt;T&amp;gt; : IFoo&amp;lt;object&amp;gt; {&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;public T Property3 { get { ... } set { ... } }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;object IFoo&amp;lt;object&amp;gt;.Property3 { get { return Property3; } }&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in; TEXT-INDENT: 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;…&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Code style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 0in 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;o:p&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 0in 0in 3pt 12.25pt; TEXT-INDENT: -12.25pt"&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN class=checkbox&gt;&lt;SPAN style="FONT-SIZE: 11pt; COLOR: #00b050; FONT-FAMILY: Wingdings; mso-bidi-font-family: ZapfDingbats; mso-ascii-font-family: ZapfDingbats; mso-hansi-font-family: ZapfDingbats; mso-char-type: symbol; mso-symbol-font-family: Wingdings; mso-bidi-font-size: 16.0pt"&gt;&lt;SPAN style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;þ&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="FONT-SIZE: 11pt"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN class=DO1&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;&lt;SPAN style="FONT-SIZE: 11pt; mso-bidi-font-family: MetaBlack-Roman"&gt;CONSIDER&lt;/SPAN&gt;&lt;/U&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt; using an abstract class instead of an interface to represent the root. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in 3pt 12.25pt"&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-bookmark: _Toc33351794"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt;This might sometimes be a better option as interfaces are more difficult to evolve (see section X). On the other hand there are some problem with using abstract classes for the root. Abstract class members cannot be implemented explicitly and the subtypes need to use the &lt;I style="mso-bidi-font-style: normal"&gt;new&lt;/I&gt; modifier. This makes it tricky to implement the root’s members by delegating to the generic type members. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-language: HE"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt;&lt;o:p&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 0in 0in 3pt 12.25pt; TEXT-INDENT: -12.25pt"&gt;&lt;SPAN class=checkbox&gt;&lt;SPAN style="FONT-SIZE: 11pt; COLOR: #00b050; FONT-FAMILY: Wingdings; mso-bidi-font-family: ZapfDingbats; mso-ascii-font-family: ZapfDingbats; mso-hansi-font-family: ZapfDingbats; mso-char-type: symbol; mso-symbol-font-family: Wingdings; mso-bidi-font-size: 16.0pt"&gt;&lt;SPAN style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;þ&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="FONT-SIZE: 11pt"&gt; &lt;/SPAN&gt;&lt;SPAN class=DO1&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;U&gt;&lt;SPAN style="FONT-SIZE: 11pt; mso-bidi-font-family: MetaBlack-Roman"&gt;CONSIDER&lt;/SPAN&gt;&lt;/U&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-language: HE"&gt; using a non-generic root type, if such type is already available. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt;&lt;FONT face=Verdana&gt;For example, List&amp;lt;T&amp;gt; implements IEnumerable for the purpose of simulating covariance.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=Text style="MARGIN: 3pt 0in 3pt 12.25pt"&gt;&lt;SPAN style="mso-bidi-language: HE"&gt;&lt;o:p&gt;&lt;FONT face=Verdana&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8348605" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/Design+Guidelines/default.aspx">Design Guidelines</category><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category><category domain="http://blogs.msdn.com/kcwalina/archive/tags/General+API+Design/default.aspx">General API Design</category></item><item><title>Synchronization at the Right Level</title><link>http://blogs.msdn.com/kcwalina/archive/2006/05/05/590937.aspx</link><pubDate>Fri, 05 May 2006 21:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:590937</guid><dc:creator>kcwalina</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/590937.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=590937</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=590937</wfw:comment><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Verdana&gt;I get a lot of questions about why the new generic collection interfaces (IList&amp;lt;T&amp;gt; for example) do not support synchronization. There are several reasons for it. One, probably the main, is that in practice synchronizing on the level of a single data structure is rarely useful. Rico explains this in a &lt;/FONT&gt;&lt;A HREF="/ricom/archive/category/13212.aspx"&gt;&lt;FONT face=Verdana color=#800080&gt;series of posts on locking&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;. In particular see the post on &lt;/FONT&gt;&lt;A HREF="/ricom/archive/2006/05/01/587750.aspx"&gt;&lt;FONT face=Verdana color=#800080&gt;putting your synchronization at the correct level&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=590937" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category><category domain="http://blogs.msdn.com/kcwalina/archive/tags/General+Programming/default.aspx">General Programming</category></item><item><title>C5 Collections</title><link>http://blogs.msdn.com/kcwalina/archive/2006/02/20/535609.aspx</link><pubDate>Mon, 20 Feb 2006 21:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:535609</guid><dc:creator>kcwalina</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/535609.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=535609</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=535609</wfw:comment><description>&lt;P&gt;&lt;SPAN style="FONT-FAMILY: Verdana"&gt;Peter Sestoft and his team just released a library of collections for the CLI called &lt;A href="http://www.itu.dk/research/c5/"&gt;C5 Collections&lt;/A&gt;. Unlike the &lt;A href="http://www.wintellect.com/powercollections/"&gt;Power Collections&lt;/A&gt;, this library&amp;nbsp;introduces many new abstractions that are not present in the .NET Framework. The benefit of such approach is that it allows for some powerful scenarios that are not possible if you simply build on top of the existing abstractions (riches compile time checking). The drawback is that it might not integrate with code that uses the standard abstractions as seamlessly as the Power Collections.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: Verdana"&gt;These two libraries represent two distinct philosophies of extending the Framework/CLI. They make different tradeoffs between the degree of innovation and interoperability. If you have used any of these libraries, I would appreciate you comments on how these philosophies work in practice and in general you observations on using 3rd party libraries. Do they integrate well with the Framework? How are they in terms of usability? Do they provide support for main functionality you are/were missing in the Framework?&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=535609" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>Disposable Collection</title><link>http://blogs.msdn.com/kcwalina/archive/2006/02/10/Disposable-Collection.aspx</link><pubDate>Sat, 11 Feb 2006 00:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:529852</guid><dc:creator>kcwalina</dc:creator><slash:comments>12</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/529852.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=529852</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=529852</wfw:comment><description>&lt;P&gt;&lt;FONT face=Verdana&gt;Somebody asked me today, if we have a collection that is disposable and calls dispose on all its items when the collections.Dispose method is called. You can implement such collection by inheriting from List&amp;lt;T&amp;gt; and adding Dispose logic. If you add one additional requirement that Dispose is called on the items before they are removed from the collection when the collection is cleared, you need to use Collection&amp;lt;T&amp;gt; which can be customized more than List&amp;lt;T&amp;gt;. Here is a quick custom collection that does what I am talking about.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;public class DisposableCollection&amp;lt;T&amp;gt; : Collection&amp;lt;T&amp;gt;, IDisposable where T: IDisposable {&lt;BR&gt;&amp;nbsp;&amp;nbsp; protected virtual void Dispose(bool disposing){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(disposing){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(T item in this){&lt;BR&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; item.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp; public void Dispose(){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dispose(true);&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;nbsp;&amp;nbsp; protected override void ClearItems(){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.ClearItems();&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;There are two problems with this collection though: a) what happens when an item is removed from the collection by calling Remove. b) what should the user do if they hold a reference to an item residing in the collection and then call Clear, which causes the item pointed to by the reference to be disposed:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;var col&amp;nbsp; = DisposableCollection&amp;lt;Foo&amp;gt;();&lt;BR&gt;col.Add(new Foo());&lt;BR&gt;Foo f = col[0];&lt;BR&gt;col.Clear();&lt;BR&gt;// now f points to a disposed object&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;… maybe Clear should not be disposing the items. What do you think?&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=529852" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>Immutable Collections</title><link>http://blogs.msdn.com/kcwalina/archive/2005/11/22/ImmutableCollections.aspx</link><pubDate>Tue, 22 Nov 2005 21:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:495854</guid><dc:creator>kcwalina</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/495854.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=495854</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=495854</wfw:comment><description>&lt;P&gt;&lt;FONT face=Verdana&gt;I just saw an interesting &lt;/FONT&gt;&lt;A href="http://gauss.dynalias.net/blog/ImmutableArraysAsValueObjects.aspx"&gt;&lt;FONT face=Verdana&gt;post&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt; that talks about a general pattern of immutable reference types. Good read. I highly recommend it! &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;At the end of the post the author suggests that we add “value object” generic collection to the BCL. It’s an interesting suggestion, but unfortunately not as easy as it may seem. The difficulty lays in the fact that the items in such collection also need to be immutable. String is an immutable collection of characters, which are&amp;nbsp;immutable themselves.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;One way to design this would be to add IImmutable marker interface, implement it on immutable types, and constrain the proposed ReadOnlyList&amp;lt;TImmutable&amp;gt; to TImmutable : IImuttable. Now, I am not sure the value of doing this outweighs the added complexity, but we will take a closer look.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=495854" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category><category domain="http://blogs.msdn.com/kcwalina/archive/tags/General+API+Design/default.aspx">General API Design</category></item><item><title>Why we don’t recommend using List&lt;T&gt; in public APIs</title><link>http://blogs.msdn.com/kcwalina/archive/2005/09/26/474010.aspx</link><pubDate>Mon, 26 Sep 2005 19:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:474010</guid><dc:creator>kcwalina</dc:creator><slash:comments>28</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/474010.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=474010</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=474010</wfw:comment><description>&lt;P&gt;&lt;FONT face=Verdana&gt;We don’t recommend using List&amp;lt;T&amp;gt; in public APIs for two reasons.&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Verdana&gt;List&amp;lt;T&amp;gt; is not designed to be extended. i.e. you cannot override any members. This for example means that an object returning List&amp;lt;T&amp;gt; from a property won’t be able to get notified when the collection is modified. Collection&amp;lt;T&amp;gt; lets you overrides SetItem protected member to get “notified” when a new items is added or an existing item is changed.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Verdana&gt;List&amp;lt;T&amp;gt; has lots of members that are not relevant in many scenarios. We say that List&amp;lt;T&amp;gt; is too “busy” for public object models. Imagine ListView.Items property returning List&amp;lt;T&amp;gt; with all its richness. Now, look at the actual ListView.Items return type; it’s way simpler and similar to Collection&amp;lt;T&amp;gt; or ReadOnlyCollection&amp;lt;T&amp;gt;.&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=474010" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/Design+Guidelines/default.aspx">Design Guidelines</category><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category><category domain="http://blogs.msdn.com/kcwalina/archive/tags/General+API+Design/default.aspx">General API Design</category></item><item><title>System.Collections vs. System.Collection.Generic and System.Collections.ObjectModel</title><link>http://blogs.msdn.com/kcwalina/archive/2005/09/23/Collections.aspx</link><pubDate>Sat, 24 Sep 2005 03:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:473463</guid><dc:creator>kcwalina</dc:creator><slash:comments>23</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/473463.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=473463</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=473463</wfw:comment><description>&lt;P&gt;&lt;FONT face=Verdana&gt;Many people ask how the new.NET Framework 2.0 generic collections relate to the non-generic collections we shipped before. So, here you go. The most important types are in bold font.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;List&amp;lt;T&amp;gt;&lt;/STRONG&gt; is basically a better ArrayList. It is optimized for speed, size, and power. Use it for majority of internal implementations whenever you need to store items in a container. Do not use it in public APIs. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;Dictionary&amp;lt;TKey,TValue&amp;gt;&lt;/STRONG&gt; is just a strongly typed Hashtable. Some changes were made to the hashing algorithm. The details are described &lt;a href="http://blogs.msdn.com/kcwalina/archive/2004/08/06/210297.aspx"&gt;here&lt;/A&gt;&lt;FONT face=Verdana&gt;.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;Collection&amp;lt;T&amp;gt;&lt;/STRONG&gt; is a much better CollectionBase. Use it to expose read/write collection output from public APIs. It’s in System.Collections.ObjectModel namespace. Why in a separate namespace and why such strange name? See &lt;a href="http://blogs.msdn.com/kcwalina/archive/2005/03/15/396086.aspx"&gt;here&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;STRONG&gt;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;/STRONG&gt; is a much better ReadOnlyCollectionBase. It’s in System.Collections.ObjectModel namespace.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Queue&amp;lt;T&amp;gt; and Stack&amp;lt;T&amp;gt; are equivalent to Queue and Stack.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;SortedList&amp;lt;TKey,TValue&amp;gt; is basically a generic version of SortedList.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;SortedDictionary&amp;lt;TKey,TValue&amp;gt; does not have a corresponding non-generic collection. It’s similar to SortedList and SortedList&amp;lt;TKey,TValue&amp;gt;, but it’s implemented as a balanced tree (red-black tree). Insertions are on average much faster, but some lookups are slightly slower and memory consumption is larger.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;IEnumerable&amp;lt;T&amp;gt; is just like IEnumerable, but strongly typed. One difference is that IEnumerable&amp;lt;T&amp;gt; extends IDisposable. The reasons are described &lt;a href="http://blogs.msdn.com/kcwalina/archive/2005/01/07/348876.aspx"&gt;here&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;ICollection&amp;lt;T&amp;gt; seems like ICollection, but it’s actually a very different abstraction. We found that ICollection was not very useful. At the same time, we did not have an abstraction that represented an read/write non-indexed collection. ICollection&amp;lt;T&amp;gt; is such abstraction and you could say that ICollection does not have an exact corresponding peer in the generic world; IEnumerable&amp;lt;T&amp;gt; is the closest.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;IList&amp;lt;T&amp;gt; is just strongly typed IList. We removed the notion of IsFixedSize and the reasons are described &lt;a href="http://blogs.msdn.com/kcwalina/archive/2005/05/18/419203.aspx"&gt;here&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;IDictionary&amp;lt;TKey,TValue&amp;gt; is roughly equivalent to IDictionary.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;DictionaryBase does not have a corresponding generic type. Simply implement IDictionary&amp;lt;TKey,TValue&amp;gt; if you need a custom dictionary.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Also, we added KeyedCollection&amp;lt;TKey,TItem&amp;gt;. It’s a new collection which allows items to be indexed by both a key and an index. Use it to expose collections of items that have natural “names” (keys) from public APIs. You need to inherit from the collection to use it.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;And finally many people asked for linked-list. LinkedList&amp;lt;T&amp;gt; was added to .NET Framework 2.0.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;So here is the summary:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;&lt;BR&gt;&lt;STRONG&gt;Non-Generic&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Similar Generic Type&lt;/STRONG&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;ArrayList&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;List&amp;lt;T&amp;gt;&lt;BR&gt;Hashtable&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dictionary&amp;lt;TKey,TValue&amp;gt;&lt;BR&gt;SortedList&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SortedList&amp;lt;TKey,TValue&amp;gt;&lt;BR&gt;Queue&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Queue&amp;lt;T&amp;gt;&lt;BR&gt;Stack&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Stack&amp;lt;T&amp;gt;&lt;BR&gt;IEnumerable&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IEnumerable&amp;lt;T&amp;gt;&lt;BR&gt;ICollection&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;N/A (use IEnumerable&amp;lt;T&amp;gt; anything that extends it)&lt;BR&gt;N/A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ICollection&amp;lt;T&amp;gt; &lt;BR&gt;IList&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IList&amp;lt;T&amp;gt;&lt;BR&gt;CollectionBase&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Collection&amp;lt;T&amp;gt;&lt;BR&gt;ReadOnlyCollectionBase&amp;nbsp;ReadOnlyCollection&amp;lt;T&amp;gt;&lt;BR&gt;DictionaryBase&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;N/A (just implement IDictionary&amp;lt;TKey,TValue&amp;gt;&lt;BR&gt;N/A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SortedDictionary&amp;lt;TKey,TValue&amp;gt;&lt;BR&gt;N/A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyedCollection&amp;lt;TKey,TItem&amp;gt;&lt;BR&gt;N/A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LinkedList&amp;lt;T&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana&gt;Let me know if you would like me&amp;nbsp;to blog more information about any of these types.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=473463" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>List&lt;T&gt; vs. ArrayList Performance</title><link>http://blogs.msdn.com/kcwalina/archive/2005/08/25/456469.aspx</link><pubDate>Fri, 26 Aug 2005 00:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:456469</guid><dc:creator>kcwalina</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/456469.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=456469</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=456469</wfw:comment><description>Rico just posted a new&amp;nbsp;&lt;a href="http://blogs.msdn.com/ricom/archive/2005/08/25/456446.aspx"&gt;performance quiz&lt;/A&gt; where he compares performance of List&amp;lt;T&amp;gt; and ArrayList in some simple scenarios. List&amp;lt;T&amp;gt; is my Whidbey baby, so of course I had to answer that List&amp;lt;T&amp;gt; is faster :-)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=456469" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>PowerCollections Beta Avaliable</title><link>http://blogs.msdn.com/kcwalina/archive/2005/06/03/425080.aspx</link><pubDate>Sat, 04 Jun 2005 02:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:425080</guid><dc:creator>kcwalina</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/425080.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=425080</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=425080</wfw:comment><description>&lt;P&gt;&lt;FONT face=Verdana&gt;The beta release of&amp;nbsp;the PowerCollections library has been posted today. See &lt;/FONT&gt;&lt;A href="http://wintellect.com/WEBLOGS/pgolde/archive/2005/06/03/1304.aspx"&gt;&lt;FONT face=Verdana&gt;http://wintellect.com/WEBLOGS/pgolde/archive/2005/06/03/1304.aspx&lt;/FONT&gt;&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=425080" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>Generic interfaces, IsReadOnly, IsFixedSize, and array</title><link>http://blogs.msdn.com/kcwalina/archive/2005/05/18/419203.aspx</link><pubDate>Wed, 18 May 2005 17:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:419203</guid><dc:creator>kcwalina</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/419203.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=419203</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=419203</wfw:comment><description>&lt;P class=MsoNormal&gt;&lt;SPAN&gt;Peter Golde recently &lt;A href="http://wintellect.com/WEBLOGS/pgolde/archive/2005/05/12/1078.aspx"&gt;posted about the IsReadOnly and IsFixedSize &lt;/A&gt;change we made to the generic collection interfaces.There are also several other discussions around the net on this topic. I thought I write up something to explain the motivation and our thinking on this. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN&gt;We decided that there are really not that many fixed size data structures that are not read only. In fact, we only have one – the array. We think that having a property on a very commonly implemented interface that is useful in only one case is not the right thing to do. So we decided to remove IsFixedSize. If we see a need for this in the future, we will add an interface called something like IIndexerSettable and implement it on fixed size structures. &lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN&gt;
&lt;P class=MsoNormal&gt;We also decided that arrays will return true from IsReadOnly, but will actually allow setting values using the indexer (which we are trying to fix in RTM as it does not work today). In other words, the IsFixedSize and IsReadOnly properties are now changed to IsReadOnly and “is Array”, like in the following example:&lt;BR&gt;&amp;nbsp;void EitherAddOrSet(IList&amp;lt;int&amp;gt; list, int newValue){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(!list.IsReadOnly) list.Add(newValue);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if(list is int[]) list[0] = newValue;&lt;BR&gt;} &lt;/P&gt;
&lt;P class=MsoNormal&gt;… oh, the IIndexerSettable interface mentioned above would work the following (and would make the EitherAddOrSet routine more general):&lt;/P&gt;
&lt;P class=MsoNormal&gt;void EitherAddOrSet(IList&amp;lt;int&amp;gt; list, int newValue){&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(!list.IsReadOnly) list.Add(newValue);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if(list is IIndexerSettable) list[0] = newValue;&lt;BR&gt;} &lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=419203" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>The reason why Collection&lt;T&gt;, ReadOnlyCollection&lt;T&gt;, and KeyedCollection&lt;TKey,TValue&gt; were moved to System.Collections.ObjectModel namespace</title><link>http://blogs.msdn.com/kcwalina/archive/2005/03/15/396086.aspx</link><pubDate>Tue, 15 Mar 2005 21:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:396086</guid><dc:creator>kcwalina</dc:creator><slash:comments>32</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/396086.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=396086</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=396086</wfw:comment><description>&lt;p class="MsoNormal" style="MARGIN-LEFT: 0.5in"&gt;&lt;font face="Verdana" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Several people asked me why Collection&amp;lt;T&amp;gt;, ReadOnlyCollection&amp;lt;T&amp;gt;, and KeyedCollection&amp;lt;TKey,TValue&amp;gt; were moved to System.Collections.ObjectModel namespace. Here are the two main reasons:&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN-LEFT: 0.75in; TEXT-INDENT: -0.25in"&gt;&lt;font face="Verdana" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;1.&lt;/span&gt;&lt;/font&gt;&lt;font size="1"&gt;&lt;span style="FONT-SIZE: 7pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;font face="Verdana" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Microsoft.VisualBasic namespace contains a non-generic type called Collection. The namespace is imported by default by the VB project template and we were afraid that it would confuse developers who import System.Collections.Generic namespace as well and would see two types called ‘Collection’ in their intellisense (given one of them generic, but still). We were faced with either renaming Collection&amp;lt;T&amp;gt; to something else or moving it to another namespace. We really did not want to rename it because Collection&amp;lt;T&amp;gt; is just the best name for the type (with not many good alternatives) and it will show in many APIs.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN-LEFT: 0.75in; TEXT-INDENT: -0.25in"&gt;&lt;font face="Verdana" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;2.&lt;/span&gt;&lt;/font&gt;&lt;font size="1"&gt;&lt;span style="FONT-SIZE: 7pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;font face="Verdana" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;We also thought that not many people will need to declare variables of thses types. Most developer who will directly use (instantiate) the collections are framework (object model) designers. Developers writing applications (majority) would simply call Directory.Files.Count for example (and not even realize it’s a Collection&amp;lt;FilerInfo&amp;gt;) or they will/should use List&amp;lt;T&amp;gt; instead. Having said that, recently I started to be concerned that some people will have to declare Collection&amp;lt;X&amp;gt; variables to cache collection instances they get from APIs, but I have not received any substantial feedback on this. BTW, this will be mitigated by many APIs actually returning subclasses of Collection&amp;lt;X&amp;gt; instead of the base class.&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN-LEFT: 0.5in"&gt;&lt;font face="Verdana" size="2"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Some people also commented that the name “ObjectModel” is a bit “kooky” (direct quote). I have received some feedback (not much but some) about it from customers, but I think the main issue is that the term is so overloaded and many people mean many different things when they say “object model”. Anyway, we would probably try to come up with some less controversial name if we were in the design phase today, but it’s kind of late in the product cycle now to make such changes. &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=396086" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>The reason why IEnumerator&lt;T&gt; extens IDisposable </title><link>http://blogs.msdn.com/kcwalina/archive/2005/01/07/348876.aspx</link><pubDate>Sat, 08 Jan 2005 02:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:348876</guid><dc:creator>kcwalina</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/348876.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=348876</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=348876</wfw:comment><description>&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Lot&lt;/span&gt;&lt;/st1:place&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;’s of people asked me why IEnumerator&amp;lt;T&amp;gt; extends IDisposable. We did this to support some obscure, yet important scenarios where an enumerator enumerates database rows, or files in a directory, etc. In such cases, the enumerator usually opens some connection or a handle, which then needs to be closed at the end of the enumeration.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Now, we could solve the problem in two ways. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;1)&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;We could do what we did. Now, foreach loop can simply call Dispose when the loop terminates. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1; tab-stops: list .5in"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-fareast-font-family: Verdana; mso-bidi-font-family: Verdana"&gt;&lt;span style="mso-list: Ignore"&gt;2)&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;We could implement foreach loop to do a dynamic cast to IDisposable at the end of the loop and if the enumerator happened to implement IDisposable, we would call it. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;We chose #1 because it’s slightly faster and the cost of implementing an empty Dispose method, for those who don’t need it, is relatively low. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;… besides, have you heard about the new C# &lt;a href="http://msdn.microsoft.com/msdnmag/issues/04/05/c20/default.aspx"&gt;yield &lt;/a&gt;statement?&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=348876" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>Power Collections on GotDotNet Featured Sites</title><link>http://blogs.msdn.com/kcwalina/archive/2004/11/03/251909.aspx</link><pubDate>Thu, 04 Nov 2004 00:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:251909</guid><dc:creator>kcwalina</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/251909.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=251909</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=251909</wfw:comment><description>&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;I just noticed that the Power Collections project made it to the “Featured Sites” on GotDotNet. See &lt;a href="http://www.gotdotnet.com/content/featuredsite/powercollections/default.aspx"&gt;http://www.gotdotnet.com/content/featuredsite/powercollections/default.aspx&lt;/a&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;For those who don’t know, &lt;a href="http://www.wintellect.com/powercollections/"&gt;Power Collections&lt;/a&gt; is a community open source library of advanced collection classes and algorithms. The project is co-sponsored by Microsoft. It has generated lots of good APIs (you can download a working library &lt;a href="http://www.wintellect.com/powercollections/download.aspx"&gt;here&lt;/a&gt;) and lots of feedback for the BCL team on how to we should be designing our collections. &lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;I would encourage anybody with a passion for collection libraries to get involved. Power Collections will influence the .NET Framework collections and so it would be great if Power Collections were influenced by the broader community.&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;You can find the specs of the APIs at &lt;a href="http://www.wintellect.com/powercollections/spec.aspx"&gt;http://www.wintellect.com/powercollections/spec.aspx&lt;/a&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;… and provide feedback at the forum &lt;a href="http://www.wintellect.com/forum/main.asp?CAT_ID=7"&gt;http://www.wintellect.com/forum/main.asp?CAT_ID=7&lt;/a&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=251909" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>IComaprer&lt;T&gt; &amp; IComparable&lt;T&gt; Refactoring Proposal</title><link>http://blogs.msdn.com/kcwalina/archive/2004/10/27/248847.aspx</link><pubDate>Thu, 28 Oct 2004 04:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:248847</guid><dc:creator>kcwalina</dc:creator><slash:comments>23</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/248847.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=248847</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=248847</wfw:comment><description>&lt;h1 style="MARGIN: 12pt 0in 3pt"&gt;&lt;a name="_Toc86650660"&gt;&lt;font size="5"&gt;&lt;font color="#ffffff"&gt;&lt;font style="BACKGROUND-COLOR: #003366"&gt;&lt;font face="Arial Black"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/a&gt;&lt;/h1&gt; &lt;p&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;We are exploring the possiblility of changing the design of IComparer&amp;lt;T&amp;gt; and IComparable&amp;lt;T&amp;gt; interfaces that will ship with Whidbey. This post describes some more detail on the issues we are trying to address with the design change and we are looking for feedback on the proposal.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana"&gt;&lt;a name="_Toc86650661"&gt;&lt;font face="Verdana" size="6"&gt;&lt;strong&gt;Background&lt;/strong&gt;&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;The first version of the .NET Framework had three main comparison related-interfaces: IComparable, IComparer, and IHashCodeProvider. One of the main issues with the interfaces was that there was no single interface that could be used by Hashtable to override the default identity of the keys (change which keys compare to equal and which not). You needed to use IHashCodeProvider to provide custom hash codes for the keys and IComparer to provide custom equality (via Compare==0). This resulted in a common usage mistake (a bug) where a user would pass incompatible pair of IHashCodeProvider and IComparer to the Hashtable constructor. For example, a&amp;nbsp;case sensitive hash code provider and a case insensitive comparer. This would result in a bug where two strings that do compare equal could have different hash codes, and so the Hashtable indexer could not find the item with the given key.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;We tried to fix this problem in Whidbey generic APIa and decided to put all comparison related methods from IComarer and IHashCodeProvider into a single interface (IComparer&amp;lt;T&amp;gt;), so users could not pass two incompatible implementations of the interfaces to the Hashtable constructor. We have always seen the new design as a compromise between a very clean OO design and trying to avoid explosion of the number of interfaces. &lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt"&gt;&lt;font size="2"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana"&gt;Well, we have received feedback from people using the new interfaces (IComparer&amp;lt;T&amp;gt; and IComparable&amp;lt;T&amp;gt;) that made us thinking that may be we compromised a bit too much &lt;/font&gt;&lt;/span&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;&lt;span style="FONT-FAMILY: Wingdings; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana; mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;&lt;span style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;J&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Verdana"&gt; Here are the relevant links describing the issues:&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;ul style="MARGIN-TOP: 0in" type="disc"&gt; &lt;li class="MsoNormal" style="MARGIN: 0in 0in 6pt; tab-stops: list .5in; mso-list: l2 level1 lfo2"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;Peter Golde (who creates &lt;/font&gt;&lt;/span&gt;&lt;a href="http://www.wintellect.com/powercollections/"&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;span style="mso-ansi-font-size: 10.0pt"&gt;power collection library&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/a&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;) gave as his feedback here: &lt;/font&gt;&lt;/span&gt;&lt;a href="http://wintellect.com/WEBLOGS/pgolde/archive/2004/07/06/180.aspx"&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;span style="mso-ansi-font-size: 10.0pt"&gt;http://wintellect.com/WEBLOGS/pgolde/archive/2004/07/06/180.aspx&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/a&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;. &lt;/font&gt;&lt;/span&gt; &lt;li class="MsoNormal" style="MARGIN: 0in 0in 6pt; tab-stops: list .5in; mso-list: l2 level1 lfo2"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;There is also a MSDN feedback thread on this at &lt;/font&gt;&lt;/span&gt;&lt;a href="http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3c698098-9290-4443-a924-97e173a6e37d"&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;span style="mso-ansi-font-size: 10.0pt"&gt;http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=3c698098-9290-4443-a924-97e173a6e37d&lt;/span&gt;&lt;/span&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/a&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;We decided to give it one more shot to fix the concerns. You can find the proposal for the new design below. &lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;I think the change proposal addresses most of the concerns, but at the same time it has the following drawbacks:&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;ul style="MARGIN-TOP: 0in" type="disc"&gt; &lt;li class="MsoNormal" style="MARGIN: 0in 0in 6pt; tab-stops: list .5in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;More interfaces to understand. Makes the Framework less approachable.&lt;/font&gt;&lt;/span&gt; &lt;li class="MsoNormal" style="MARGIN: 0in 0in 6pt; tab-stops: list .5in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;Custom comparers can implement one interface (let’s say IEqualityProvider&amp;lt;T&amp;gt;) but not the other (IComparer&amp;lt;T&amp;gt;). Switching from one collection which uses IEqualityProvider&amp;lt;T&amp;gt; to another which happens to use IComparer&amp;lt;T&amp;gt; may be difficult (as you will need a different custom comparer). &lt;/font&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt; tab-stops: list .5in; mso-list: l0 level1 lfo3"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt;&lt;/font&gt;&lt;/span&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;I am looking for feedback on the proposal from people who have experimented with the interfaces. The change is something that we would do only if there is an overwhelming positive feedback from the community, not if the improvement&amp;nbsp;seems only marginal.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;Thanks!&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 6pt"&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;/span&gt;&lt;span style="mso-bookmark: _Toc86650660"&gt;&lt;font face="Verdana" size="2"&gt;&lt;/font&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt; &lt;h1 style="MARGIN: 12pt 0in 3pt"&gt;&lt;a name="_Toc86650661"&gt;&lt;font face="Verdana"&gt;Current Design&lt;/font&gt;&lt;/a&gt;&lt;/h1&gt; &lt;p class="codeCxSpFirst" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IComparable&amp;lt;T&amp;gt; {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool Equals(T other);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int CompareTo(T other);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IComparer&amp;lt;T&amp;gt; {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool Equals(T x, T y);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;int Compare(T x, T y);&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;int GetHashCode(T obj);&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;o:p&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IKeyComparer : IComparer, IHashCodeProvider {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool Equals(object x, object y);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpLast" style="MARGIN: 0in 0in 6pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;h2 style="MARGIN: 12pt 0in 3pt"&gt;&lt;a name="_Toc86650662"&gt;&lt;font face="Verdana"&gt;Proposed New Design&lt;/font&gt;&lt;/a&gt;&lt;/h2&gt; &lt;h4 style="MARGIN: 0in 0in 6pt"&gt;&lt;font face="Verdana"&gt;IComparable&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/h4&gt; &lt;p class="BulletedList" style="MARGIN: 0in 0in 6pt 0.25in"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font size="2"&gt;·&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Verdana" size="2"&gt;Break into two interfaces&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpFirst" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IEquateable&amp;lt;T&amp;gt; {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;bool Equals(T other);&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;o:p&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IComparable&amp;lt;T&amp;gt; {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int CompareTo(T other);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpLast" style="MARGIN: 0in 0in 6pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;h4 style="MARGIN: 0in 0in 6pt"&gt;&lt;font face="Verdana"&gt;IComparer&amp;lt;T&amp;gt;&lt;/font&gt;&lt;/h4&gt; &lt;p class="BulletedList" style="MARGIN: 0in 0in 6pt 0.25in"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font size="2"&gt;·&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Verdana" size="2"&gt;Break into two interfaces&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpFirst" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IEqualityProvider&amp;lt;T&amp;gt; {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool Equals(T x, T y);&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;int GetHashCode(T obj);&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;o:p&gt;&lt;font face="Courier New" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IComparer&amp;lt;T&amp;gt; {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;int Compare(T x, T y);&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpLast" style="MARGIN: 0in 0in 6pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;h4 style="MARGIN: 0in 0in 6pt"&gt;&lt;font face="Verdana"&gt;IKeyComparer&lt;/font&gt;&lt;/h4&gt; &lt;p class="BulletedListCxSpFirst" style="MARGIN: 0in 0in 0pt 0.25in"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font size="2"&gt;·&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Verdana" size="2"&gt;Rename from IKeyComparer to IHashComparer&lt;/font&gt;&lt;/p&gt; &lt;p class="BulletedListCxSpLast" style="MARGIN: 0in 0in 6pt 0.25in"&gt;&lt;span style="FONT-FAMILY: Symbol; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"&gt;&lt;span style="mso-list: Ignore"&gt;&lt;font size="2"&gt;·&lt;/font&gt;&lt;span style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;font face="Verdana" size="2"&gt;Remove IComparer requirement (base type)&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpFirst" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;public interface IEqualityProvider : IHashCodeProvider {&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpMiddle" style="MARGIN: 0in 0in 0pt; mso-add-space: auto"&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;bool Equals(object x, object y);&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="codeCxSpLast" style="MARGIN: 0in 0in 6pt; mso-add-space: auto"&gt;&lt;font face="Courier New" size="2"&gt;}&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=248847" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item><item><title>System.Collections.Generic Dictionary&lt;K,V&gt; Capacity, Hashing, and Collisions.</title><link>http://blogs.msdn.com/kcwalina/archive/2004/08/06/210297.aspx</link><pubDate>Sat, 07 Aug 2004 03:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:210297</guid><dc:creator>kcwalina</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/kcwalina/comments/210297.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kcwalina/commentrss.aspx?PostID=210297</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kcwalina/rsscomments.aspx?PostID=210297</wfw:comment><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Somebody asked me a question how to set the initial capacity of &lt;A href="http://longhorn.msdn.microsoft.com/lhsdk/ref/ns/system.collections.generic/c/dictionary/dictionary.aspx"&gt;System.Collections.Generic.Dictionary&amp;lt;K,V&amp;gt;&lt;/A&gt; if one knows that the Dictionary will contain 1000 entries.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;SPAN style="FONT-FAMILY: Verdana"&gt;The short answer&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Dictionary&amp;lt;int,string&amp;gt; numbers = new Dictionary&amp;lt;int,string&amp;gt;(1000); &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;SPAN style="FONT-FAMILY: Verdana"&gt;The long answer&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;SPAN style="FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Dictionary&amp;lt;K,V&amp;gt; is a generic type almost identical to the non-generic &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionshashtableclasstopic.asp"&gt;System.Collections.Hashtable&lt;/A&gt;. Both types are implemented as &lt;A href="http://en.wikipedia.org/wiki/Hashtable"&gt;hashtables&lt;/A&gt;, but they are slightly different implementations. In particular, they use different algorithms to deal with &lt;A href="http://en.wikipedia.org/wiki/Hash_collision"&gt;collisions&lt;/A&gt;. Hashtable uses &lt;B style="mso-bidi-font-weight: normal"&gt;probing&lt;/B&gt; and Dictionary&amp;lt;K,V&amp;gt; uses &lt;B style="mso-bidi-font-weight: normal"&gt;chaining&lt;/B&gt;.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Probing&lt;/SPAN&gt;&lt;/B&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt; means that when a collision is detected, the algorithm tries to store the new entry in the next free bucket. When a hashtable gets close to full (saturated), finding the next free bucket becomes difficult. For this reason, hashtables that use probing grow their size way before they get completely saturated to minimize collisions. In our Hashtable, the threshold of acceptable saturation is controlled by the loadFactor parameter the constructor, but this is a topic for another time. Hashtables using probing always have more buckets allocated than the number of entries they store. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Now, the interesting question is whether the capacity specifies the number of buckets we allocate or the algorithm is actually smarter. The good news is that we take the loadFactor into account when allocating the buckets. So, if you specify capacity of let&amp;#8217;s say 1000, we actually allocate more buckets. In fact we allocate enough buckets to ensure that we don&amp;#8217;t have to grow the number of buckets before more entries are added than the specified capacity.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;B style="mso-bidi-font-weight: normal"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;Chaining&lt;/SPAN&gt;&lt;/B&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt; means that all entries with the same hash code are stored in a list associated with one bucket corresponding to the hash code. Therefore the Dictionary&amp;lt;K,V&amp;gt; can be completely saturated, we don&amp;#8217;t use loadFactor, and we simply take the next prime number after the capacity to determine how many buckets to allocate.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=210297" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kcwalina/archive/tags/System.Collections/default.aspx">System.Collections</category></item></channel></rss>