<?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>Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx</link><description>When people start using C# generics for the first time, they are sometimes surprised that they can’t convert between related generic instances. For example, since you can convert a string to an object , shouldn’t you also be able to convert a List &amp;lt;string&amp;gt;</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Mindless Link Propagation</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#375771</link><pubDate>Fri, 18 Feb 2005 04:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375771</guid><dc:creator>.Net Security Blog</dc:creator><description /></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#381704</link><pubDate>Mon, 28 Feb 2005 17:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:381704</guid><dc:creator>Paul Tessier</dc:creator><description>After using generics for that last month, I know I can never go back again.  Right now through, I'd kill for exactly what you have in your &amp;quot;theoretical extension of C#&amp;quot;.&lt;br&gt;&lt;br&gt;I've writen:&lt;br&gt;class A&amp;lt;T&amp;gt;&lt;br&gt;{&lt;br&gt;void F&amp;lt;T2&amp;gt;(A&amp;lt;T2&amp;gt; val) where T2 : T&lt;br&gt;}&lt;br&gt;&lt;br&gt;I'm going crazy.&lt;br&gt;</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#532748</link><pubDate>Thu, 16 Feb 2006 00:05:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:532748</guid><dc:creator>dcoetzee</dc:creator><description>Hi Rick. Today a tester walked into my office and complained to me that he can't convert a List of derived classes to a List of the base class. When I explained that that would violate type safety because List objects are not read-only, he asked if he could just wrap it in some kind of read-only adapter which would then be covariant. He should be able to, but the sad answer is no - it would be great to have this simple kind of OO type support in C# generics. Definitely something to push for in C# 3.0.</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#532762</link><pubDate>Thu, 16 Feb 2006 00:22:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:532762</guid><dc:creator>rmbyers</dc:creator><description>Yep, that's exactly what you'd want to be able to do. &amp;nbsp;In fact, IEnumerable&amp;lt;T&amp;gt; is that &amp;quot;read-only wrapper&amp;quot;. &amp;nbsp;I've spoken with the C# team about this. &amp;nbsp;In fact, Mads Torgersen is the key person that got this support added to Java (in the form of wildcard types), and I was impressed to see that he now works on our C# team. &amp;nbsp;Not surprisingly, he'd like very much to be able to add this to C#, but there are additional complications in C# that make that very difficult (in Java, wildcard types were added at the same time as generics - in C# we've got a significant compatability problem, plus in Java generics exist only in the compiler, not in the run-time type system like in the CLR). &amp;nbsp;So, unfortunately, I wouldn't hold your breath waiting for this in C# 3.0.</description></item><item><title>Covariance versus Contravariance</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#587129</link><pubDate>Sun, 30 Apr 2006 18:39:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:587129</guid><dc:creator>Michael Primeaux's Blog</dc:creator><description>Lately, I've been fielding questions as related to conversion between related generic types in C# 2.0....</description></item><item><title>More on generic variance</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#616383</link><pubDate>Sun, 04 Jun 2006 02:54:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:616383</guid><dc:creator>Rick Byers</dc:creator><description>In my entry on generic variance in the CLR, I said that you can’t convert a List&amp;amp;amp;lt;String&amp;amp;amp;gt; to a List&amp;amp;amp;lt;Object&amp;amp;amp;gt;,...</description></item><item><title>Only Talking Sense &amp;raquo; C# Generics</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#636779</link><pubDate>Mon, 19 Jun 2006 16:13:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:636779</guid><dc:creator>Only Talking Sense » C# Generics</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="http://onlytalkingsense.wordpress.com/2006/06/19/c-generics/"&gt;http://onlytalkingsense.wordpress.com/2006/06/19/c-generics/&lt;/a&gt;</description></item><item><title>CoVariance, Contravariance and Generics</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#684444</link><pubDate>Mon, 31 Jul 2006 23:26:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:684444</guid><dc:creator>The Original .NET Geek</dc:creator><description>This post is aresult of an expensive lesson I learned. The situation: A MVP style architecture, Presentation</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#699482</link><pubDate>Mon, 14 Aug 2006 16:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:699482</guid><dc:creator>TheInsaneCoder</dc:creator><description>If generic casting to base generic classes is not possible, is there a way we can work around this without writting hundreds of lines of code? &lt;br&gt;&lt;br&gt;I've got in the simplest terms a lockmanager which manages locks on rows in a table. This lock manager is inherited by a lot of managers of specific tables and features. &lt;br&gt;&lt;br&gt;A generic form of mine takes editor controls and manages all the lock/save operations. If i want to provide a &amp;quot;business manager&amp;quot; to it as much as a &amp;quot;department manager&amp;quot; it won't let me do it.&lt;br&gt;&lt;br&gt;If i can't convert back to a generic lock manager to handle various types of object how can i work around that still keeping it secure and allowing only business objects or contact objects to their respective managers?</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#726511</link><pubDate>Sun, 27 Aug 2006 12:20:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:726511</guid><dc:creator>Jeorge Lukasing</dc:creator><description>Very many thanks for a good work. Nice and useful. Like it!</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#732333</link><pubDate>Wed, 30 Aug 2006 23:22:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:732333</guid><dc:creator>rmbyers</dc:creator><description>TheInsaneCoder,&lt;br&gt;Sorry for the delay - I've been out of the office for the past few weeks (just had my first child!). &amp;nbsp;&lt;br&gt;&lt;br&gt;Casting (and even implicit conversions) to base generic classes IS possible. &amp;nbsp;Eg., if Bar&amp;lt;T&amp;gt; derives from Foo&amp;lt;T&amp;gt; then you can convert a Foo&amp;lt;int&amp;gt; to a Bar&amp;lt;int&amp;gt;. &amp;nbsp;&lt;br&gt;&lt;br&gt;What we're talking about here is converting between different generic instantiations (eg. List&amp;lt;Foo&amp;gt; to List&amp;lt;Bar&amp;gt;). &amp;nbsp;In general this is not safe, and if you find yourself wanting to do this a lot it often means there is a flaw in your generic type design (eg. you don't really want the strong typing generics provide).&lt;br&gt;&lt;br&gt;To mix between the strong-typing and weak-typing world, you often want to have generic classes implement non-generics interfaces. &amp;nbsp;This allows you to throw away the extra type information embodied by the generic type parameters when its appropriate to do so.&lt;br&gt; Eg., List&amp;lt;T&amp;gt; implements the non-generic IList and IEnumerable interfaces for exactly this reason.&lt;br&gt;&lt;br&gt;Its hard to say for sure without a more detailed description (and/or code) of what you're trying to do. &amp;nbsp;But it sounds to me like you perhaps want your generic lock managers to implement a non-generic lock manager interface.</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#744944</link><pubDate>Thu, 07 Sep 2006 23:09:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:744944</guid><dc:creator>cherridge</dc:creator><description>My real confusion (and i'm sure this is what your describing) is why this doesn't work...&lt;br&gt;&lt;br&gt;I'm only casting the generic type with it's T parameter going back to an interface that T derives from...&lt;br&gt;&lt;br&gt;Am i missing something here or is it just impossible in c#2?&lt;br&gt;&lt;br&gt;&lt;br&gt;interface IAmAString&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string Text {get;}&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;class MyStringClass : IAmAString&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string _Text;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public string Text&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;br&gt; &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; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (_Text);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;class GenericClassA&amp;lt;T&amp;gt; where T : IAmAString&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;class Program&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object o = new GenericClassA&amp;lt;MyStringClass&amp;gt;();&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GenericClassA&amp;lt;IAmAString&amp;gt; newo = (GenericClassA&amp;lt;IAmAString&amp;gt;)o;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//I've just thrown a casting exception&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;}</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#745006</link><pubDate>Thu, 07 Sep 2006 23:49:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:745006</guid><dc:creator>rmbyers</dc:creator><description>Cherridge,&lt;br&gt;Yes that is exactly what I'm describing. &amp;nbsp;It's intentionally not possible (if it were, you'd lose a lot of the benefits of generics).&lt;br&gt;&lt;br&gt;Specifically, if you could do that, then what would a program like this do?:&lt;br&gt;&lt;br&gt;interface IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;string Text { get;}&lt;br&gt;}&lt;br&gt;class MyStringClass : IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;string _Text;&lt;br&gt; &amp;nbsp; &amp;nbsp;public string Text&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (_Text);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;class OtherStringClass : IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;int _num;&lt;br&gt; &amp;nbsp; &amp;nbsp;public string Text&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return _num.ToString();&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;class GenericClassA&amp;lt;T&amp;gt; where T : IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;GenericClassA(T init)&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_field = init;&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;&lt;br&gt; &amp;nbsp; &amp;nbsp;public T _field;&lt;br&gt;}&lt;br&gt;class Program&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GenericClassA&amp;lt;MyStringClass&amp;gt; o = new GenericClassA&amp;lt;MyStringClass&amp;gt;(new MyStringClass());&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GenericClassA&amp;lt;IAmAString&amp;gt; newo = (GenericClassA&amp;lt;IAmAString&amp;gt;)o;&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// If that worked, I could now do this:&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IAmAString newString = new OtherStringClass();&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newo._field = newString;&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Now what type should this be? &amp;nbsp;Generics are supposed to allow this to work without a type-check&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// or possibility if InvalidCastException. &amp;nbsp;But we've got a OtherStringClass stored where we expect&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// to have a MyStringClass!&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyStringClass s = o._field;&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;Perhaps really what you want to write is something more like this (similar to how List&amp;lt;T&amp;gt; implements the non-generic IList interface):&lt;br&gt;&lt;br&gt;interface IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;string Text { get;}&lt;br&gt;}&lt;br&gt;class MyStringClass : IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;string _Text;&lt;br&gt; &amp;nbsp; &amp;nbsp;public string Text&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (_Text);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;interface IStringClass&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;IAmAString SomeFunc();&lt;br&gt;}&lt;br&gt;&lt;br&gt;class GenericClassA&amp;lt;T&amp;gt; : IStringClass where T : IAmAString&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;public T SomeFunc()&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp;// This function lets us decide explicitly what happens when a T is converted to a IAmAString.&lt;br&gt; &amp;nbsp; &amp;nbsp;// In this case we can just do an implicit conversion. &amp;nbsp;In other scenarios you may want a runtime-checked&lt;br&gt; &amp;nbsp; &amp;nbsp;// cast.&lt;br&gt; &amp;nbsp; &amp;nbsp;IAmAString IStringClass.SomeFunc()&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return SomeFunc();&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;class Program&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Here we can use things knowing their exact type&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GenericClassA&amp;lt;MyStringClass&amp;gt; o = new GenericClassA&amp;lt;MyStringClass&amp;gt;();&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MyStringClass s1 = o.SomeFunc();&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// But if we want to erase some of that type information, we still can:&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IStringClass newo = o;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IAmAString s = newo.SomeFunc();&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>C# generics covariance</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#1374436</link><pubDate>Thu, 28 Dec 2006 18:17:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1374436</guid><dc:creator>Andrew Stopford's Weblog</dc:creator><description>&lt;p&gt;I&amp;amp;#39;ve posted about this before , largely covariance is possible in C# delegates but not featured in&lt;/p&gt;
</description></item><item><title>Chris Donnan : Programming - Brooklyn Style -  Generics lack covariance/ contravariance</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#1591640</link><pubDate>Sat, 03 Feb 2007 21:29:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1591640</guid><dc:creator>Chris Donnan : Programming - Brooklyn Style -  Generics lack covariance/ contravariance</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://chrisdonnan.com/blog/2007/02/03/generics-lack-covariance-contravariance/"&gt;http://chrisdonnan.com/blog/2007/02/03/generics-lack-covariance-contravariance/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Invari</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#3293897</link><pubDate>Thu, 14 Jun 2007 20:19:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3293897</guid><dc:creator>Nuno Filipe Godinho</dc:creator><description /></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#4891270</link><pubDate>Thu, 13 Sep 2007 12:20:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4891270</guid><dc:creator>cisjokey</dc:creator><description>&lt;P&gt;Hello dear&lt;/P&gt;
&lt;P&gt;I'hope i can post something usefull. I'had the same problem as you all. And (in some cases) its easy to come around;&lt;/P&gt;
&lt;P&gt;I make a sample with a "Searcher" class;&lt;/P&gt;
&lt;P&gt;class Searcher&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;public ICollection&amp;lt;ISearchItem&amp;gt; Search(IList&amp;lt;ISearchItem&amp;gt; listToSearch)&lt;/P&gt;
&lt;P&gt;{ //searches through the items }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;Now if you have a list with items of a type that implements this ISearchItem interface, you get the compiler-errors as you described above. &lt;/P&gt;
&lt;P&gt;e.g List&amp;lt;SearchItem&amp;gt;&lt;/P&gt;
&lt;P&gt;what you could do is the following thing;&lt;/P&gt;
&lt;P&gt;class Searcher&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;public ICollection&amp;lt;T&amp;gt; Search&amp;lt;T&amp;gt;(IList&amp;lt;T&amp;gt; listToSearch) where T:ISearchItem&lt;/P&gt;
&lt;P&gt;{ //searches through the items }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;And now, you can use your Interface without writing hundreds of lines of code. You do not see which type is expected with intellisense when using this Searchmethod, but the method is at least "typesave".&lt;/P&gt;
&lt;P&gt;If this post is useless or misplaced/misinterpreted, just delete it (im not that programming geek and do not know if this is a suiteable solution).&lt;/P&gt;
&lt;P&gt;Greetings&lt;/P&gt;
&lt;P&gt;Cis&lt;/P&gt;</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#4895823</link><pubDate>Thu, 13 Sep 2007 18:47:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4895823</guid><dc:creator>rmbyers</dc:creator><description>&lt;p&gt;Cis,&lt;/p&gt;
&lt;p&gt;Yes - that's exactly right, in some cases you can use a generic method with a base class constraint to overcome this limitation. &amp;nbsp;I talked about this option in detail in my follow-up post here: &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/rmbyers/archive/2006/06/01/613690.aspx"&gt;http://blogs.msdn.com/rmbyers/archive/2006/06/01/613690.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Generic type parameter variance in the CLR</title><link>http://blogs.msdn.com/rmbyers/archive/2005/02/16/generic-type-parameter-variance-in-the-clr.aspx#6208552</link><pubDate>Wed, 14 Nov 2007 15:14:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6208552</guid><dc:creator>richardcollins</dc:creator><description>&lt;p&gt;I would certainly like to see this kind of extension made to C#, but I would prefer the syntax to be, e.g.:&lt;/p&gt;
&lt;p&gt;interface IEnumerable&amp;lt;out T&amp;gt; { ... }&lt;/p&gt;
&lt;p&gt;interface IComparer&amp;lt;in T&amp;gt; { ... }&lt;/p&gt;
&lt;p&gt;as this describes the constraints more clearly.&lt;/p&gt;
&lt;p&gt;-Richard.&lt;/p&gt;</description></item></channel></rss>