<?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>LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx</link><description>This post covers the upcoming C# 4.0 support for covariance and contravariance when working with delegates and interfaces. Eric Lippert’s series of posts on this subject are definitely the definitive reference at this time. I’m writing this overview of</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9020644</link><pubDate>Tue, 28 Oct 2008 21:06:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9020644</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9022370</link><pubDate>Wed, 29 Oct 2008 17:57:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9022370</guid><dc:creator>Josh Berke</dc:creator><description>&lt;p&gt;Couple of questions for you:&lt;/p&gt;
&lt;p&gt;Can a type param be both in and out so would the following be allowed:&lt;/p&gt;
&lt;p&gt;public interface ICopier&amp;lt;in out T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; T Copy(t item);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Also can you explain why the need for the extra keywords? I can't imagine why but I am sure there is a reason. &lt;/p&gt;
&lt;p&gt;thanks&lt;/p&gt;
&lt;p&gt;Josh&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9022851</link><pubDate>Wed, 29 Oct 2008 22:23:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9022851</guid><dc:creator>iCe</dc:creator><description>&lt;p&gt;I'm with Josh, please explain us why we need those in-out keywords. I can't figure out why is needed to do something that is what you expect, specially when talking about covariance&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9023024</link><pubDate>Thu, 30 Oct 2008 00:17:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023024</guid><dc:creator>Daedius</dc:creator><description>&lt;p&gt;I think I understand the background. &amp;nbsp;This is a feature created to support F# functional programming types. &amp;nbsp;Immutable functions without side effects is a HUGE thing in functional programming. &amp;nbsp;The contravariant delegates are a way of representing these functions in C#.&lt;/p&gt;
&lt;p&gt;Basically, delegates with &amp;quot;in&amp;quot; parameters are delegates that can produce no side effects. &amp;quot;out&amp;quot; delegates are the more common garden variety delegates we all know and love.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9023129</link><pubDate>Thu, 30 Oct 2008 01:10:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023129</guid><dc:creator>iCe</dc:creator><description>&lt;p&gt;Daedius, in this example the &amp;quot;out&amp;quot; keyword is not a &amp;quot;common delegate&amp;quot;. Indeed, it is making something that actually cannot be done &amp;quot;covariance&amp;quot;.&lt;/p&gt;
&lt;p&gt;delegate T Func1&amp;lt;out T&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;delegate void Action1&amp;lt;in T&amp;gt;(T a);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Covariance&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func1&amp;lt;Cat&amp;gt; cat = () =&amp;gt; new Cat();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func1&amp;lt;Animal&amp;gt; animal = cat;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Contravariance&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Action1&amp;lt;Animal&amp;gt; act1 = (ani) =&amp;gt; { Console.WriteLine(ani); };&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Action1&amp;lt;Cat&amp;gt; cat1 = act1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;What I&amp;#180;m really want to know, is why the compiler cannot do this automatically, simply by allowing you do covariance and contravariance without any keyword at all. I think if it was possible actually in some parts of the language why not in generics and delegates? As Charlie has said &amp;quot;In Visual Studio 2010 delegates will behave as expected&amp;quot;, so if it's something we expect, why we are forced to use a special keyword to get expected behavior?&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9023611</link><pubDate>Thu, 30 Oct 2008 07:22:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023611</guid><dc:creator>Halo_Four</dc:creator><description>&lt;p&gt;It is a limitation in the name of type safety. &amp;nbsp;You are declaring that the interface or delegate can accept base or derived classes of the generic parameter by explicitly defining that said interface or delegate will either only allow T as input, or only allow T as output. &amp;nbsp;The interface or delegate cannot be both.&lt;/p&gt;
&lt;p&gt;If you are using covariance it means that the interface or delegate can output a value that is a class of the specified type parameter or derived from that class. &amp;nbsp;If you pass a Func&amp;lt;Cat&amp;gt; to a method that expects a Func&amp;lt;Animal&amp;gt; it works fine since it will return a Cat, which is derived from Animal. &amp;nbsp;That method can then treat the Cat as if it were an Animal without any ill effects.&lt;/p&gt;
&lt;p&gt;If you are using contravariance it means that the interface or delegate will receive input of a value that is a class of the specified type parameter or derived from that class. &amp;nbsp;If you pass an Action&amp;lt;Animal&amp;gt; to a method that expects an Action&amp;lt;Cat&amp;gt; it works fine as it will pass a Cat which is derived from Animal. &amp;nbsp;The body of the called delegate can treat the Cat as if it were an Animal without any ill effects.&lt;/p&gt;
&lt;p&gt;These roles are pretty specific and cannot be reversed. &amp;nbsp;A method that takes a Func&amp;lt;Cat&amp;gt; cannot accept a Func&amp;lt;Animal&amp;gt; because the Animal that it returns might not be a valid Cat and it would fail. &amp;nbsp;Similarly a method that takes an Action&amp;lt;Cat&amp;gt; cannot accept an Action&amp;lt;Animal&amp;gt; for the same reason. &amp;nbsp;The new keywords really only apply to the implementers of fairly fundamental structures, such as IEnumerable&amp;lt;out T&amp;gt; or Action&amp;lt;in T&amp;gt;. &amp;nbsp;Their benefit comes offering the flexibility in the consumption of types that consume those structures. &amp;nbsp;You might never use those keywords, but because of them you can now do this:&lt;/p&gt;
&lt;p&gt;// Illegal in C# 3.0&lt;/p&gt;
&lt;p&gt;IEnumerable&amp;lt;Animal&amp;gt; animals = new List&amp;lt;Cat&amp;gt;();&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9023711</link><pubDate>Thu, 30 Oct 2008 08:56:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023711</guid><dc:creator>Bruce Pierson</dc:creator><description>&lt;p&gt;&amp;quot;IEnumerable&amp;lt;Animal&amp;gt; animals = new List&amp;lt;Cat&amp;gt;();&amp;quot;&lt;/p&gt;
&lt;p&gt;This is HUGE. I can't believe how many special interfaces I have to use to get true &amp;quot;programming to an interface&amp;quot; right now just because of this limitation.&lt;/p&gt;
&lt;p&gt;For example, right now I have to do this:&lt;/p&gt;
&lt;p&gt;Domain object, with CRUD or Mapper ops (much easier to code to a concrete or abstract class collection than to an interface that has no CRUD methods):&lt;/p&gt;
&lt;p&gt;public IList&amp;lt;RuleState&amp;gt; ChildStates&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;get { return m_ChildStates; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;then in the interface for my runtime configurator (on the same class), I have to do this:&lt;/p&gt;
&lt;p&gt;IList&amp;lt;IRuntimeRuleState&amp;gt; IRuntimeConfig.GetRuleStates()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; List&amp;lt;IRuntimeRuleState&amp;gt; list = new List&amp;lt;IRuntimeRuleState&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;foreach(IRuntimeRuleState rs in m_ChildStates)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;list.Add(rs);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return list;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;In c# 4, I could just do:&lt;/p&gt;
&lt;p&gt;IList&amp;lt;IRuntimeRuleState&amp;gt; IRuntimeConfig.GetRuleStates()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; return m_ChildStates;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Much easier to code, not to mention far more efficient.&lt;/p&gt;
&lt;p&gt;Can't wait.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9023730</link><pubDate>Thu, 30 Oct 2008 09:08:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023730</guid><dc:creator>Bruce Pierson</dc:creator><description>&lt;p&gt;Your comment about this &amp;quot;acting as the programmer expects&amp;quot; is exactly right. I was very excited about generics, but my excitement was tempered when I found out I couldn't do:&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;private List&amp;lt;SomeObject&amp;gt; m_Field;&lt;/p&gt;
&lt;p&gt;public IList&amp;lt;ISomeObject&amp;gt; Prop&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;get { return m_Field; }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;...&lt;/p&gt;
&lt;p&gt;As I posted previously, this covariance fix will alleviate this problem, and make us all better programmers by giving us the tools to be better programmers.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9023994</link><pubDate>Thu, 30 Oct 2008 11:49:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9023994</guid><dc:creator>int19h</dc:creator><description>&lt;p&gt;Unfortunately, it won't alleviate your problem specifically, because IList&amp;lt;T&amp;gt; cannot be covariant. The reason is that it uses T both in arguments of methods - such as &amp;quot;void IList&amp;lt;T&amp;gt;.Add(T)&amp;quot;; and in return values - such as &amp;quot;T IList&amp;lt;T&amp;gt;.this[int]&amp;quot;. This means that you won't be able to write this in C# 4.0:&lt;/p&gt;
&lt;p&gt;interface IFoo { ... }&lt;/p&gt;
&lt;p&gt;class Foo : IFoo { ... }&lt;/p&gt;
&lt;p&gt;IList&amp;lt;IFoo&amp;gt; list = new List&amp;lt;Foo&amp;gt;();&lt;/p&gt;
&lt;p&gt;In fact, at a quick glance, there are only a few basic types in the FCL that would be covered by new variance declarations: Predicate&amp;lt;T&amp;gt;, Comparer&amp;lt;T&amp;gt;, Action&amp;lt;...&amp;gt; and Func&amp;lt;...&amp;gt;, IEnumerable, IEquatable, IComparable. ICollection&amp;lt;T&amp;gt; and anything further down the inheritance chain will have to remain invariant. &lt;/p&gt;
&lt;p&gt;As a result, I doubt that we'll see &amp;quot;in&amp;quot; and &amp;quot;out&amp;quot; in application code in class declarations often (if at all). On the other hand, it will probably become good style to always use them when declaring delegate types.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9024058</link><pubDate>Thu, 30 Oct 2008 12:15:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024058</guid><dc:creator>Luis Abreu</dc:creator><description>&lt;p&gt;I'm with int19h on this one. Since you cannot have both in and out applied to the parameter, it seems like you won't be able to use covariance/contravariance with the most interesting collections...&lt;/p&gt;
&lt;p&gt;btw, should I assume that we won't be getting Design By Contract on C# 4.0? Why isn't this part of tha language?It's one of those things that should have been there for a long time!&lt;/p&gt;
</description></item><item><title>C# 4.0: Covariance and Contravariance on generics…sort of…</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9024120</link><pubDate>Thu, 30 Oct 2008 12:44:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024120</guid><dc:creator>LA.NET [EN]</dc:creator><description>&lt;p&gt;One of the things that we’ll have in C# 4.0 is covariance/contravariace working on generics…I mean, sort&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9024161</link><pubDate>Thu, 30 Oct 2008 13:10:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024161</guid><dc:creator>OOP Sceptic</dc:creator><description>&lt;p&gt;Really guys, this is so far away from what programming really means, ie working with real people to find out what they want and then implementing it that it's a total waste of time. &lt;/p&gt;
&lt;p&gt;Has anyone here actually done a programming for a living (and I don't count writing programming books or similar wanky stuff as programming)&lt;/p&gt;
</description></item><item><title>C# 4.0: Covariance and Contravariance on generics…sort of…</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9024200</link><pubDate>Thu, 30 Oct 2008 13:45:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024200</guid><dc:creator>ASPInsiders</dc:creator><description>&lt;p&gt;One of the things that we’ll have in C# 4.0 is covariance/contravariace working on generics…I mean, sort&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9024824</link><pubDate>Thu, 30 Oct 2008 20:05:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024824</guid><dc:creator>iCe</dc:creator><description>&lt;p&gt;Thanks Halo_Four, you have cleared a lot of things. &lt;/p&gt;
&lt;p&gt;So the problem here is with classes that output and input the same generic parameter, so that is why it cannot be done without the &amp;quot;in&amp;quot; and &amp;quot;out&amp;quot; keywords. Also note that as Bruce Pierson and int19h commented, unfortunately this will not cover all the expected cases, like those when the parameter is used both in and out.&lt;/p&gt;
&lt;p&gt;OOP Sceptic I&amp;#180;m in a real world project right know and those discussions allow me to understand better the tool that I&amp;#180;m using every day. And my better understanding of the tool means better products for the client, and better support.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9024890</link><pubDate>Thu, 30 Oct 2008 20:36:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9024890</guid><dc:creator>Bruce Pierson</dc:creator><description>&lt;p&gt;Thanks, int19h, for the clarification, even though it's depressing...&lt;/p&gt;
&lt;p&gt;@OOP Sceptic&lt;/p&gt;
&lt;p&gt;I currently have many customers using my software to actually run their manufacturing businesses (boats, trailers, clothing, and more), and it suffers from being difficult to modify to suit their needs because of the lack of flexible design patterns, and old-style procedural programming. I'm currently re-writing it using expert design pattern guidance, and I cannot believe the difference it makes to my sanity when I take the time to understand and apply these principles.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9025066</link><pubDate>Thu, 30 Oct 2008 22:04:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9025066</guid><dc:creator>Morten Mertner</dc:creator><description>&lt;p&gt;This is indeed very promising and will be helpful in many scenarios, but I'm dissapointed to see that it doesn't solve the problems with ICollection and IList.&lt;/p&gt;
&lt;p&gt;Would it not be possible to define a new set of collection interfaces without this problem?&lt;/p&gt;
&lt;p&gt;Or alternatively, add a mechanism that allows specifying the variance at the method level instead of at type level, so that when a generic type parameter is used for both in and out the specific use (for a method) can be explicitly specified.&lt;/p&gt;
&lt;p&gt;It may be necessary or desired to also specify the variance on consumption, similar to how ref/out must be specified both at declaration and on invocation. &lt;/p&gt;
&lt;p&gt;Mind you, I don't usually think about these things and Eric clearly lives in another dimension.. :)&lt;/p&gt;
&lt;p&gt;That said, surely there must be some way to allow people to write &amp;quot;IList&amp;lt;IFoo&amp;gt; list = new List&amp;lt;Foo&amp;gt;();&amp;quot; as this is such a common pattern. I'll take compiler magic like for &amp;quot;int? i = null&amp;quot; over any &amp;quot;will not compile&amp;quot; error.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9025170</link><pubDate>Thu, 30 Oct 2008 23:03:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9025170</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;You can't allow &amp;quot;IList&amp;lt;IFoo&amp;gt; list = new List&amp;lt;Foo&amp;gt;();&amp;quot;, otherwise you could end up with:&lt;/p&gt;
&lt;p&gt;class Foo : IFoo {}&lt;/p&gt;
&lt;p&gt;class Bar : IFoo {}&lt;/p&gt;
&lt;p&gt;IList&amp;lt;IFoo&amp;gt; list = new List&amp;lt;Foo&amp;gt;();&lt;/p&gt;
&lt;p&gt;list.Add(new Bar());&lt;/p&gt;
&lt;p&gt;The only option would be to split the interface declaration into four parts - non-generic (Clear, Count, IsReadOnly, RemoveAt), covariant (GetEnumerator, indexer), contravariant (Add, Contains, IndexOf, Insert, Remove) and invariant (CopyTo).&lt;/p&gt;
&lt;p&gt;Note that CopyTo would have to be invariant because it accepts an array of type T. You can only pass an array of U to a method which expects an array of type T if U is derived from or equal to T, but you can only put an object of type T in an array of type U if T is derived from or equal to U. Therefore, the only type U which satisfies both conditions is the type T, and the method must be invariant.&lt;/p&gt;
&lt;p&gt;I think that this also explains why the compiler can't automatically infer the &amp;quot;variance-ness&amp;quot; of a type parameter.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;p&gt;static void Fill(IFoo[] values)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;values[0] = new Foo();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;values[1] = new Bar();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Fill(new object[2]); // Compiler error&lt;/p&gt;
&lt;p&gt;Fill(new Foo[2]); // ArrayTypeMismatchException&lt;/p&gt;
&lt;p&gt;Fill(new Bar[2]); // ArrayTypeMismatchException&lt;/p&gt;
&lt;p&gt;Fill(new IFoo[2]); // Works&lt;/p&gt;
&lt;p&gt;And if you can follow that gibberish, you've probably had too much coffee! ;)&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9025206</link><pubDate>Thu, 30 Oct 2008 23:16:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9025206</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;Sorry - I obviously meant to say that the indexer get would be covariant, while the indexer set would be contravariant.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9025274</link><pubDate>Fri, 31 Oct 2008 00:03:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9025274</guid><dc:creator>Thomas Krause</dc:creator><description>&lt;p&gt;Nice to finally see this in the language, but the choice of keywords is just horrible:&lt;/p&gt;
&lt;p&gt;1.) we already have the out keyword for parameters, but with completly different semantics. And generic type &amp;quot;parameters&amp;quot; are similar enough to function parameters to confuse many people, especially if they are new to the language.&lt;/p&gt;
&lt;p&gt;2.) without looking I cannot tell which one of the keywords is used for contravariance and which is used for covariance. &amp;quot;In&amp;quot; and &amp;quot;out&amp;quot; is not vocabulary I'd normally use when talking about type hierachy relations.&lt;/p&gt;
&lt;p&gt;3.) there are far better alternatives. Why not simply use &amp;quot;super&amp;quot; and &amp;quot;sub&amp;quot; instead. It is far easier to remember that &amp;quot;sub T&amp;quot; means you can use subtypes of T and &amp;quot;super T&amp;quot; means you can use supertypes of T.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9026113</link><pubDate>Fri, 31 Oct 2008 10:24:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9026113</guid><dc:creator>int19h</dc:creator><description>&lt;p&gt;Actually, &amp;quot;in&amp;quot; and &amp;quot;out&amp;quot; are fairly obvious because they clearly outline the restrictions on the usage of a type parameter. An &amp;quot;in&amp;quot; type parameter can only be used for values that are inputs of methods - that is, non-out/ref arguments. An &amp;quot;out&amp;quot; type parameter can only be used for values that are outputs of methods - return values, and out-arguments&lt;/p&gt;
&lt;p&gt;&amp;gt; Would it not be possible to define a new set of collection interfaces without this problem?&lt;/p&gt;
&lt;p&gt;It is possible if you basically split each collection interface into three parts: covariant, contravariant, and both. E.g. for a list:&lt;/p&gt;
&lt;p&gt;interface IListVariant&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int Count { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool IsReadOnly { get; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;void Clear();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;void RemoveAt(int);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;interface IListCovariant&amp;lt;out T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;T Item[int] { get; set; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp;IEnumerator&amp;lt;T&amp;gt; GetEnumerator();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;interface IListContravariant&amp;lt;in T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;void Add(T);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool Contains(T);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;void CopyTo(T[], int);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int IndexOf(T);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;void Insert(int, T);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;bool Remove(T);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;interface IList&amp;lt;T&amp;gt; : IListVariant&amp;lt;T&amp;gt;, IListCovariant&amp;lt;T&amp;gt;, IListContravariant&amp;lt;T&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;And so on for all other collection types (and all other interfaces that could also be so split). So far I haven't seen any indication that this is going to happen in .NET 4.0 (at least it's not mentioned on the &amp;quot;what's new in 4.0&amp;quot; poster), and, looking at the code above, I think it is understandable :)&lt;/p&gt;
&lt;p&gt;&amp;gt; Or alternatively, add a mechanism that allows specifying the variance at the method level instead of at type level, so that when a generic type parameter is used for both in and out the specific use (for a method) can be explicitly specified.&lt;/p&gt;
&lt;p&gt;I will repeat what I said elsewhere, and just say that the proper way to enable full variance is to do what Java guys did, and use variance markers at use-site, not at declaration-site, same as Java does it. For example, here are two methods that use IList&amp;lt;T&amp;gt; differently:&lt;/p&gt;
&lt;p&gt;// Method can take IList&amp;lt;T&amp;gt; where T is string or any base class of string&lt;/p&gt;
&lt;p&gt;void AddString(IList&amp;lt;in string&amp;gt; list, string s)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// cannot use any methods of list that return values of type T here, only those who take arguments of type T&lt;/p&gt;
&lt;p&gt; &amp;nbsp;list.Add(s);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;//list[0]; // illegal here!&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;// Method can take IList&amp;lt;T&amp;gt; where T is object or any class derived from object&lt;/p&gt;
&lt;p&gt;object GetFirst(IList&amp;lt;out object&amp;gt; list, int i)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;// cannot use any methods of list that take arguments of type T here, only those that return values of type T&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return list[i];&lt;/p&gt;
&lt;p&gt; &amp;nbsp;//list.Add(s); // illegal here!&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9026268</link><pubDate>Fri, 31 Oct 2008 12:21:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9026268</guid><dc:creator>OOP Sceptic</dc:creator><description>&lt;p&gt;@ iCe and Bruce Pierson&lt;/p&gt;
&lt;p&gt;Thanks for responding. I too have many hundreds of people running their finance businesses on my software.&lt;/p&gt;
&lt;p&gt;I appreciate that advances in any particular technology are going to improve that technology, BUT it seems to me that all these somewhat esoteric terminologies are just solutions waiting for problems.&lt;/p&gt;
&lt;p&gt;Maybe this new stuff will help you, but it's a little late in the day for magic solutions - surely these wacko extension merely highlight the flaws in OOP any way!&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9026474</link><pubDate>Fri, 31 Oct 2008 14:58:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9026474</guid><dc:creator>Richard</dc:creator><description>&lt;p&gt;@ int19h:&lt;/p&gt;
&lt;p&gt;CopyTo can't be contravariant, as I tried to explain above. Passing an array is equivalent to passing a ref parameter. Although the method shouldn't read from the array, there's nothing in the declaration to prevent it.&lt;/p&gt;
&lt;p&gt;Although it would be nice if &amp;quot;out&amp;quot; parameters could be used in contravariant interfaces, I don't think the CLR would support it. As I understand it, the only difference between &amp;quot;out&amp;quot; and &amp;quot;ref&amp;quot; parameters is the C# compiler's definite assignment checks.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9035231</link><pubDate>Tue, 04 Nov 2008 00:40:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9035231</guid><dc:creator>leblanc meneses</dc:creator><description>&lt;p&gt;&amp;gt;As I say, contravariance is a bit confusing. &lt;/p&gt;
&lt;p&gt;maybe the names are weird but usage is just polymorphism (some interface is expected).&lt;/p&gt;
&lt;p&gt;I can already see this is going get missused. &amp;nbsp;instead of using a factory pattern for Covariance&lt;/p&gt;
&lt;p&gt;and aggregation to implement Contravariance. (pass you concrete object implementing an interface into another object that will make it work) &lt;/p&gt;
&lt;p&gt;i wish there was some real world usage in these examples..&lt;/p&gt;
</description></item><item><title>Community Convergence XLVII</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9036492</link><pubDate>Tue, 04 Nov 2008 04:37:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9036492</guid><dc:creator>Charlie Calvert's Community Blog</dc:creator><description>&lt;p&gt;Welcome to the 47th Community Convergence. We had a very successful trip to PDC this year. In this post&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9040984</link><pubDate>Tue, 04 Nov 2008 22:15:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9040984</guid><dc:creator>int19h</dc:creator><description>&lt;p&gt;&amp;gt; instead of using a factory pattern for Covariance and aggregation to implement Contravariance. (pass you concrete object implementing an interface into another object that will make it work)&lt;/p&gt;
&lt;p&gt;Since covariance and contravariance in C# 4.0 will work only on interfaces (and delegates, which are semantically really just one-method interfaces), I don't see your point.&lt;/p&gt;
&lt;p&gt;In fact, I don't understand it at all. How would aggregation help deal with the present problem that IEnumerable&amp;lt;Derived&amp;gt; cannot be treated as (i.e. cast to) IEnumerable&amp;lt;Base&amp;gt;, even though it is clearly typesafe and meaningful to do so?&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9045233</link><pubDate>Thu, 06 Nov 2008 00:38:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9045233</guid><dc:creator>Josh Berke</dc:creator><description>&lt;p&gt;@OOP: I think learning about aditions to the language is a great way for us to learn new ways to applys technology solutions through code to our business problems. While I was very skeptical of Linq at first, I have come to enjoy Linq to Objects, as a quick way to filter and sort my collections (Especially when binding to grids). &lt;/p&gt;
&lt;p&gt;@Everyone Else: I still don't get the need for the new key words, and I'm afraid unless I sat down with an expert who could pound it into my head with a base ball bat I won't get it. Since I'm the sole developer at my company, guess I'll have to finally go and attend a community event.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
</description></item><item><title>Thoughts on C# 4.0 and .NET 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9054813</link><pubDate>Sun, 09 Nov 2008 03:59:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9054813</guid><dc:creator>James Newton-King</dc:creator><description>&lt;p&gt;C# 4.0 Dynamic Lookup I really like the way the C# team tackled bring dynamic programming to the language&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9058492</link><pubDate>Mon, 10 Nov 2008 22:03:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9058492</guid><dc:creator>leblanc meneses</dc:creator><description>&lt;p&gt;&amp;gt; IEnumerable&amp;lt;Derived&amp;gt; cannot be treated as (i.e. cast to) IEnumerable&amp;lt;Base&amp;gt;, even though it is clearly typesafe and meaningful to do so?&lt;/p&gt;
&lt;p&gt;just so all can see what you mean, here is the test:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;class Test&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public Test()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;List&amp;lt;Base&amp;gt; items = new List&amp;lt;Base&amp;gt;(this.GetItems());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IEnumerable&amp;lt;Derived&amp;gt; GetItems()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return new Derived();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class Base&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public class Derived : Base&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;it fails to compile.&lt;/p&gt;
&lt;p&gt;I agree semantically it 'could' compile.&lt;/p&gt;
&lt;p&gt;On purpose during the Symantic Analysis stage the compiler won't let it compile.&lt;/p&gt;
&lt;p&gt;Why?&lt;/p&gt;
&lt;p&gt;You should be returning:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IEnumerable&amp;lt;Base&amp;gt; GetItems()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return new Derived();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;so you never couple higher layers with concrete types.&lt;/p&gt;
&lt;p&gt;not IEnumerable&amp;lt;Derived&amp;gt; which is not meaningful since the Test class only needs the Base interface.&lt;/p&gt;
&lt;p&gt;again i wish there was some real world examples on why this is actually needed.&lt;/p&gt;
</description></item><item><title>Co\Contra-Variance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9060957</link><pubDate>Wed, 12 Nov 2008 02:22:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9060957</guid><dc:creator>Andrew Stopford's Weblog</dc:creator><description>&lt;p&gt;Running behind on everything again, picked up a nasty stomach bug that laid me out for a few days (not&lt;/p&gt;
</description></item><item><title>C# 4.0 New Features</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9062683</link><pubDate>Wed, 12 Nov 2008 17:01:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9062683</guid><dc:creator>Guy     kolbis</dc:creator><description>&lt;p&gt;There are a few new features coming out in C# 4.0. I gathered some posts that will help you to &amp;amp;quot;get&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9237450</link><pubDate>Thu, 18 Dec 2008 23:26:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9237450</guid><dc:creator>tytusse</dc:creator><description>&lt;p&gt;As someone pointed much earlier (so I am not 1st) - the keywords are necessary - you cant now - at runtime - what is the actual generic type of generic collection, and furthermore - using &amp;quot;generic&amp;quot; cast you will loose type information which is necessary for compile time checks and leads to stupid runtime fails, of which I dreamed to forget when I first saw generics...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public class Base&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public class Derived : Base&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public class Tests {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void Test1() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; List&amp;lt;Derived&amp;gt; derivedList = new List&amp;lt;Derived&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; List&amp;lt;Base&amp;gt; baseList = derivedList; // sounds fair?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; baseList.Add(new Base()); // wrong, collection is actually of of Derived type, and...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Derived d = derivedList[0]; // should actually make an implicit up-cast to work (and fail in this case)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;So you cant know, at run time, what type is safe for generic cast, unless you limit this type to use &amp;quot;T&amp;quot; only in input or only in output method parameters.&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9237457</link><pubDate>Thu, 18 Dec 2008 23:27:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9237457</guid><dc:creator>tytusse</dc:creator><description>&lt;p&gt;As someone pointed much earlier (so I am not 1st) - the keywords are necessary - you cant now - at runtime - what is the actual generic type of generic collection, and furthermore - using &amp;quot;generic&amp;quot; cast you will loose type information which is necessary for compile time checks and leads to stupid runtime fails, of which I dreamed to forget when I first saw generics...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public class Base&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public class Derived : Base&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; public class Tests {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static void Test1() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; List&amp;lt;Derived&amp;gt; derivedList = new List&amp;lt;Derived&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; List&amp;lt;Base&amp;gt; baseList = derivedList; // sounds fair?&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; baseList.Add(new Base()); // wrong, collection is actually of of Derived type, and...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Derived d = derivedList[0]; // should actually make an implicit up-cast to work (and fail in this case)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt;So you cant know, at run time, what type is safe for generic cast, unless you limit this type to use &amp;quot;T&amp;quot; only in input or only in output method parameters.&lt;/p&gt;
</description></item><item><title>그외 새로 들어가는 IDE 기능들</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9374993</link><pubDate>Sun, 25 Jan 2009 17:41:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9374993</guid><dc:creator>HeeJae's Blog</dc:creator><description>&lt;p&gt;전에 쓴 post에 있는 새로운 IDE 기능은 dynamic과 COM interop에 관련되어 새로 추가된 기능들이고, 당연히 이 밖에도 여러가지 새로 VS10에 추가 되는 IDE&lt;/p&gt;
</description></item><item><title>re: LINQ Farm: Covariance and Contravariance in C# 4.0</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9554365</link><pubDate>Fri, 17 Apr 2009 18:59:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9554365</guid><dc:creator>OOP Master</dc:creator><description>&lt;p&gt;Wow, that's intense stuff. Thanks for the summary. Sure, I'll learn about lambduh's, dynamic and functional C# programming features, but seriously, I haven't had OOP problems in C# where I even need to know how to spell variance, covariance and contravariance. Oh, and I have personally released hundred's of thousands of lines of pure C# in production right now for my clients, just one app right now is 557,000 loc that. I bet I could reduce the loc, but at what price, maintainability and readability? No thanks. Small focused classes lead naturally to composition, which is far superior to inheritance for most pattern implementations to get green tests and keep them green.&lt;/p&gt;
</description></item><item><title>C# INVARIANZA, COVARIANZA &amp; CONTRAVARIANZA</title><link>http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx#9572510</link><pubDate>Tue, 28 Apr 2009 03:12:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9572510</guid><dc:creator>MVP Factor</dc:creator><description>&lt;p&gt;Nuestro buen amigo Pete que ya nos ha compartido en el paso muy buenos art&amp;#237;culos de XNA, ahora nos comparte&lt;/p&gt;
</description></item></channel></rss>