<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Quickduck &#187; Contravariant</title>
	<atom:link href="http://quickduck.com/blog/tag/contravariant/feed/" rel="self" type="application/rss+xml" />
	<link>http://quickduck.com/blog</link>
	<description>Straight from the mind of geniuseseses....</description>
	<lastBuildDate>Wed, 30 Jun 2010 10:52:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>C# 3.0 &#8211; Variance Explained</title>
		<link>http://quickduck.com/blog/2009/07/30/c-3-0-variance-explained/</link>
		<comments>http://quickduck.com/blog/2009/07/30/c-3-0-variance-explained/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 00:20:13 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Contravariance]]></category>
		<category><![CDATA[Contravariant]]></category>
		<category><![CDATA[Covariance]]></category>
		<category><![CDATA[Covariant]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Invariance]]></category>
		<category><![CDATA[Invariant]]></category>
		<category><![CDATA[Subclass]]></category>
		<category><![CDATA[Variance]]></category>

		<guid isPermaLink="false">http://quickduck.com/blog/?p=121</guid>
		<description><![CDATA[The problem: Why can’t I create a List of type Dog and assign it to a List of type Animal? IList&#60;Animal&#62; animals = new List&#60;Dog&#62;(); // no good Theory: There are 3 terms relating to variance: Covariance – allows more specific types to be assigned to more general types. (i.e. sub-types (classes, interfaces) can be [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The problem:</strong></p>
<p>Why can’t I create a List of type Dog and assign it to a List of type Animal?</p>
<pre><code>IList&lt;Animal&gt; animals = new List&lt;Dog&gt;(); // no good</code></pre>
<p><strong>Theory:</strong></p>
<p>There are 3 terms relating to variance:</p>
<p><strong>Covariance</strong> – allows more specific types to be assigned to more general types. (i.e. sub-types (classes, interfaces) can be assigned to any types (classes, interfaces) that they inherit from).</p>
<p><em>C# Example:</em> Method Return types are Covariant. We can return a sub-type of the method’s declaring return type.</p>
<pre>
<code>
IAnimal GetAnimal(string animalName) {...};

GetAnimal("dog") {return new Dog();} // the dog is more specific and returned as the general type IAnimal
GetAnimal("cat") {return new Cat();} // the cat is more specific and returned as the general type IAnimal
</code>
</pre>
<p><strong>Contravariance</strong> – allows general types to accept more specific types – i.e. The reverse of covariance.</p>
<p><em>C# Example:</em> Method parameters are Contravariant. We can call a method with a parameter that is a sub-type of the parameters declaring type.</p>
<pre>
<code>
IAnimal GetAnimal(IAnimal animal) {...};
GetAnimal(new Dog()); // the method takes a general type IAnimal but is called with the more specific type Dog
GetAnimal(new Cat()); // the method takes a general type IAnimal but is called with the more specific type Dog
</code>
</pre>
<p><strong>Invariance</strong> – occurs when neither of these conditions are met.</p>
<p><em>C# Example:</em> In C# 3.0 Generics are invariant. C# 4.0 allows the variance of generics to be defined (with restrictions).</p>
<pre>
<code>
IList&lt;Animal&gt; animals = new IList&lt;Animal&gt;();
animals.Add(new Dog());
animals.Add(new Cat()); 

IList&lt;Animal&gt; animals = new List&lt;Dog&gt;();
animals.Add(new Dog());
animals.Add(new Cat()); // no dice. You can’t assign a cat to a list of dogs.
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://quickduck.com/blog/2009/07/30/c-3-0-variance-explained/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
