﻿<?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>Programming By A Tool &#187; Covariance</title>
	<atom:link href="http://byatool.com/tag/covariance/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>And now for Contravariance</title>
		<link>http://byatool.com/lessons/and-now-for-contravariance/</link>
		<comments>http://byatool.com/lessons/and-now-for-contravariance/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 12:43:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Contravariance]]></category>
		<category><![CDATA[Covariance]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/29/and-now-for-contravariance/</guid>
		<description><![CDATA[Take the classes in the last post (First, Second, Third) and assume they are exactly the same in this example. In Covariance, we learned that whatever variable to you set equal to the return of a method has to be equal in type of larger. Or in other words, it has to have equal or [...]]]></description>
			<content:encoded><![CDATA[<p>Take the classes in the last post (<span style="color: #00cccc;">First</span>, <span style="color: #00cccc;">Second</span>, <span style="color: #00cccc;">Third</span>) and assume they are exactly the same in this example.</p>
<p>In Covariance, we learned that whatever variable to you set equal to the return of a method has to be equal in type of larger. Or in other words, it has to have equal or less functionality.</p>
<pre><span style="color: #00cccc;">  Second</span> second = ReturnThird();  <span style="color: #006600;">//OK since second has less functionality</span>

<span style="color: #00cccc;">  Third</span> third = ReturnSecond(); <span style="color: #006600;">//BAD since third has more functionality</span></pre>
<p>Now I think you can guess what Contravariance is, but if you can&#8217;t it&#8217;s ok. Most likely you&#8217;re a tool just like me. Contravariance is the movement from small to large meaning that the type must be equal to or larger than. Following the &#8220;in other words&#8221; manor, it has to have equal or more functionality.</p>
<p>Now small note before I go on, saying that it has to have more/less functionality can be somewhat dangerous. After all, <span style="color: #00cccc;">Third</span> could inherit from <span style="color: #00cccc;">Second</span> and add no functionality, but I find this is an easier way to think of it. I suppose another way of thinking of it is that with Covariance the return type has to have equal or more knowledge. Meaning, <span style="color: #00cccc;">Second</span> has full knowledge of what <span style="color: #00cccc;">First</span> is, but <span style="color: #00cccc;">Second</span> has no idea what <span style="color: #00cccc;">Third</span> is.</p>
<p>Anywho, onto some examples. Say we take the FillX methods and add something to it.</p>
<pre><span style="color: #3333ff;">  private</span> <span style="color: #3333ff;">void</span> FillFirst(<span style="color: #00cccc;">First</span> firstToFill)
  {
    firstToFill.FirstOutput = <span style="color: #cc0000;">""</span>;
  }

<span style="color: #3333ff;">  private</span> <span style="color: #3333ff;">void</span> FillSecond(<span style="color: #00cccc;">Second</span> secondToFill)
  {
    secondToFill.SecondOutput = <span style="color: #cc0000;">""</span>;
  }

<span style="color: #3333ff;">  private</span> <span style="color: #3333ff;">void</span> FillThird(<span style="color: #00cccc;">Third</span> thirdToFill)
  {
    thirdToFill.ThirdOutput = <span style="color: #cc0000;">""</span>;
  }</pre>
<p>Right off the bat you might notice that if methods allowed Covariance with parameters, you&#8217;d be in trouble. After all, if FillThird allowed parameter covariance, you could pass in a <span style="color: #00cccc;">First</span> object. What what that object do with ThirdOutPut? As things are, you would have a bad day. Lucky for you, at least if you aren&#8217;t adamant about wanting Covariance in parameters, this can&#8217;t happen.</p>
<p>Well shoot, I just gave away the fun of this post. Oh well, I&#8217;ll keep going in case you just have more time.</p>
<pre><span style="color: #00cccc;">  Action</span>&lt;<span style="color: #00cccc;">First</span>&gt; fillFirstAction = FillFirst;
<span style="color: #006600;">  //No problems here since FillFirst expects a First</span>

<span style="color: #00cccc;">  Action</span>&lt;<span style="color: #00cccc;">Second</span>&gt; fillSecondAction = FillFirst;
<span style="color: #006600;">  //Still no problems although this may look odd.  But remember, FillFirst</span>
<span style="color: #006600;">  //just needs an object that : First, it doesn't care if the object</span>
<span style="color: #006600;">  //has more functionality than first.</span>
<span style="color: #006600;">  //</span>
<span style="color: #006600;">  //The FillFirst method uses the FirstOutput property and by inheritance</span>
<span style="color: #006600;">  //the Second being passed in has said property</span>

<span style="color: #00cccc;">  Action</span>&lt;<span style="color: #00cccc;">Second</span>&gt; fillThirdAction = FillThird;
<span style="color: #006600;">  //Not gonna happen.  The FillThird expects a third or smaller object.  Since</span>
<span style="color: #006600;">  //Third : Second, third is smaller than second.  Implications?  Look in the </span>
<span style="color: #006600;">  //FillThirdMethod</span>
<span style="color: #006600;">  //</span>
<span style="color: #006600;">  //The method expects the object to have the ThirdOutput property which means</span>
<span style="color: #006600;">  //Second has to inherit from Third.  We know this to be untrue.</span></pre>
<p>So basically Contravariance is used with parameters in methods to guarantee the object being passed in has at least the functionality used within the method.</p>
<p>Apparently there was a problem in the lobby so there will be no refreshments served tonight.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/and-now-for-contravariance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Covariance versus Contravariance</title>
		<link>http://byatool.com/lessons/covariance-versus-contravariance/</link>
		<comments>http://byatool.com/lessons/covariance-versus-contravariance/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 18:26:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Covariance]]></category>
		<category><![CDATA[Func]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/28/covariance-versus-contravariance/</guid>
		<description><![CDATA[Ok so I stumbled on to this subject the other day and thought it was worth noting. Take these simple classes: public class First { public String FirstOutput { get; set; } } public class Second : First { public String SecondOutput { get; set; } } public class Third : Second { public String [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so I stumbled on to this subject the other day and thought it was worth noting. Take these simple classes:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">class</span> <span style="color: #00cccc;">First</span>
{
  <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">String</span> FirstOutput { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
}

<span style="color: #3333ff;">public</span> <span style="color: #3333ff;">class</span> <span style="color: #00cccc;">Second</span> : <span style="color: #00cccc;">First</span>
{
  <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">String</span> SecondOutput { <span style="color: #3366ff;">get</span>; <span style="color: #3366ff;">set</span>; }
}

<span style="color: #3333ff;">public</span> <span style="color: #3333ff;">class</span> <span style="color: #00cccc;">Third</span> : <span style="color: #00cccc;">Second</span>
{
  <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">String</span> ThirdOutput { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
}</pre>
<p>So from this you can see that <span style="color: #00cccc;">Third</span> inherits <span style="color: #00cccc;">Second</span> which in turns inherits <span style="color: #00cccc;">First</span>. By terminology this would mean that <span style="color: #00cccc;">Third</span> is &#8220;smaller&#8221; than <span style="color: #00cccc;">Second</span> and <span style="color: #00cccc;">First</span> is &#8220;larger&#8221; than both. Here&#8217;s an example of Covariance:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">class</span> <span style="color: #00cccc;">Covariance</span>
{
  <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">Covariance</span>()
  {

      <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">First</span>&gt; returnFirstFunc = ReturnFirst;
      <span style="color: #009900;">//This works since the Func has a return type of First</span>

      <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Second</span>&gt; returnSecondFunc = ReturnThird;
      <span style="color: #00cccc;">Second</span> secondTest = returnSecondFunc();
      secondTest.FirstOutput = <span style="color: #990000;">"First"</span>;
      secondTest.SecondOutput = <span style="color: #990000;">"First"</span>;
      <span style="color: #006600;">//This works since the Func has a return type of Third which is smaller</span>
<span style="color: #006600;">       //that Second.  Therefore anyone using this Func will expect a Second to</span>
<span style="color: #006600;">       //be returned and will only use the methods/properties that a Second object</span>
<span style="color: #006600;">       //would have.  Methods/Properties that Third has by inheritance.</span>

      <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Third</span>&gt; returnThirdFunc = ReturnSecond;
      <span style="color: #006600;">//THIS WILL NOT WORK</span>
<span style="color: #006600;">       //Due to Covariance, the return of the method must be equal or smaller</span>
<span style="color: #006600;">       //that the expected type.  returnThirdFunc expects a Third or smaller object</span>
<span style="color: #006600;">       //but the ReturnSecond method returns a Second which is not smaller than Third.</span>
<span style="color: #006600;">       //Afterall, Third : Second</span>
<span style="color: #006600;">       //</span>
<span style="color: #006600;">       //Third thirdTest = returnThirdFunc();</span>
<span style="color: #006600;">       //Is the same as:</span>
<span style="color: #006600;">       //Third thirdTest = new Second();</span>
  }

  <span style="color: #3333ff;">private</span> <span style="color: #00cccc;">First</span> ReturnFirst()
  {
      <span style="color: #3333ff;">return</span> <span style="color: #3333ff;">new</span> First();
  }

  <span style="color: #3333ff;">private</span> <span style="color: #00cccc;">Second</span> ReturnSecond()
  {
      <span style="color: #3333ff;">return</span> <span style="color: #3333ff;">new</span> Second();
  }

  <span style="color: #3333ff;">private</span> <span style="color: #00cccc;">Third</span> ReturnThird()
  {
      <span style="color: #3333ff;">return</span> <span style="color: #3333ff;">new</span> Third();
  }
}</pre>
<p>Basically what this all means is that with return types, the return type must be smaller or equal to the field it&#8217;s being set to. When you are dealing with Funcs, the return type must be smaller or equal to the return type for the method it&#8217;s being set it. Why is that? Well think of it like this:</p>
<p>It&#8217;s your first day on the job and some guy tells you to write something with whatever returnFirstFunc() returns. Now you have no way to look at the code, so you can only know that it returns <span style="color: #00cccc;">First</span>. For all you know, it could return <span style="color: #00cccc;">First</span>, <span style="color: #00cccc;">Second</span>, or <span style="color: #00cccc;">Third</span>. So you would do this:</p>
<pre><span style="color: #00cccc;">First</span> someFirst;

someFirst = returnFirstFunc();  <span style="color: #006600;">//Could return anything smaller than First</span>
someFirst.FirstOutput;  <span style="color: #006600;">//Completely legal and safe</span></pre>
<p>But would you do this?</p>
<pre>someFirst.ThirdOutput;</pre>
<p>Of course not since you only can assume it is a <span style="color: #00cccc;">First</span>. Now let&#8217;s do this in reverse. Say from the above example you were allowed to do this:</p>
<pre><span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Third</span>&gt; returnThirdFunc = ReturnSecond;</pre>
<p>Could you do this?</p>
<pre><span style="color: #00cccc;">Third</span> third;

third = returnThirdFunc();
third.ThirdOutput;</pre>
<p>Yeah you can&#8217;t since the <span style="color: #00cccc;">Second</span> type doesn&#8217;t have the ThirdOutput property.</p>
<p>In short Covariance is the allowance of Smaller types or equal. If a method returns back Third, then you can use that method for anything that is <span style="color: #00cccc;">Third</span> or Smaller (<span style="color: #00cccc;">Second</span>, <span style="color: #00cccc;">First</span>, <span style="color: #00cccc;">Object</span>) but not for something Larger (<span style="color: #00cccc;">Fourth</span>, <span style="color: #00cccc;">Fifth</span>, ect).</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/covariance-versus-contravariance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

