﻿<?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; Func</title>
	<atom:link href="http://byatool.com/tag/func/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>Linq Extension Methods Versus Linq Query Language&#8230; DEATHMATCH</title>
		<link>http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/</link>
		<comments>http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 03:09:38 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=86</guid>
		<description><![CDATA[Today I was writing out an example of why the extension methods are for the most part better to use than the querying language. Go figure I would find a case where that&#8217;s not entirely true. Say you are using these three funcs: Func&#60;User, String&#62; userName = user =&#62; user.UserName; Func&#60;User, Boolean&#62; userIDOverTen = user [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was writing out an example of why the extension methods are for the most part better to use than the querying language.  Go figure I would find a case where that&#8217;s not entirely true.  Say you are using these three funcs:</p>
<pre><span style="color: #33cccc;">    Func</span>&lt;<span style="color: #33cccc;">User</span>, <span style="color: #33cccc;">String</span>&gt; userName = user =&gt; user.UserName;
<span style="color: #33cccc;">    <span>Func</span></span>&lt;<span style="color: #33cccc;">User</span>, <span style="color: #33cccc;">Boolean</span>&gt; userIDOverTen = user =&gt; user.UserID &lt; 10;
<span style="color: #33cccc;">    <span>Func</span></span>&lt;<span style="color: #33cccc;">User</span>, <span style="color: #33cccc;">Boolean</span>&gt; userIDUnderTen = user =&gt; user.UserID &gt; 10;</pre>
<p>As you can see the first one replaces the lamdba expression to get the user name, the second replaces a lamdba expression used to check if the ID is lower than 10, and let&#8217;s face it, the third should be pretty easy to understand now.</p>
<p>NOTE:  This is a  silly example but it works.</p>
<pre><span style="color: #33cccc;">    </span><span style="color: #0000ff;">var</span> userList =
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">from</span> user <span style="color: #0000ff;">in</span> userList
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">where</span> userIDOverTen(user)
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">select</span> userName;
Versus

<span style="color: #33cccc;">    </span><span style="color: #0000ff;">var</span> otherList =
<span style="color: #33cccc;">      </span>userList
<span style="color: #33cccc;">      </span>.Where(IDIsBelowNumber)
<span style="color: #33cccc;">      </span>.Select(userName)</pre>
<p>In this example, the second is a little less verbose since the extension method can make full use of the Func, but he Linq expression can&#8217;t since it is look just for a Boolean rather than a Func that returns boolean.   However, this is where it might be better to use the expression language.  Say you already had a method that takes in more than just a user:</p>
<pre><span style="color: #33cccc;">    </span><span style="color: #0000ff;">private</span> <span style="color: #33cccc;">Boolean</span> IDIsBelowNumber(<span style="color: #33cccc;">User</span> user, <span style="color: #33cccc;">Int32</span> someNumber, <span style="color: #33cccc;">Boolean</span> doSomething)
<span style="color: #33cccc;">    </span>{
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">return</span> user.UserID &lt; someNumber;
    }</pre>
<p>Note:  doSomething is just there because of the where extension method being ok with a method that takes in a user and integer and returns boolean.  Kind of annoying for this example.</p>
<p>Now if you look at the Linq query:</p>
<pre><span style="color: #33cccc;">    </span><span style="color: #0000ff;">var</span> completeList =
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">from</span> user <span style="color: #0000ff;">in</span> userList
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">where</span> userIDOverTen(user, 10)
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">select</span> userName;</pre>
<p>You&#8217;re good for it.  Now the Extension Method:</p>
<pre><span style="color: #33cccc;">    </span><span style="color: #0000ff;">var</span> otherList =
<span style="color: #33cccc;">      </span>userList
<span style="color: #33cccc;">      </span>.Where(IDIsBelowNumber????)
<span style="color: #33cccc;">      </span>.Select(userName)</pre>
<p>Without a lambda expression, I really can&#8217;t call that method.  So now what I have to do is create a method that creates a Func based off the original method call.</p>
<pre><span style="color: #33cccc;">    </span><span style="color: #0000ff;">private</span> <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">User</span>, <span style="color: #33cccc;">Boolean</span>&gt; IDIsBelowNumberFunc(<span style="color: #33cccc;">Int32</span> number)
<span style="color: #33cccc;">    </span>{
<span style="color: #33cccc;">      </span><span style="color: #0000ff;">return</span> user =&gt; IDIsBelowNumber(user, number, <span style="color: #0000ff;">true</span>);
<span style="color: #33cccc;">    </span>}</pre>
<p>And then plug it in:</p>
<pre><span style="color: #33cccc;">    </span><span style="color: #0000ff;">var</span> otherList =
<span style="color: #33cccc;">      </span>userList
<span style="color: #33cccc;">      </span>.Where(IDIsBelowNumberFunc(10))
<span style="color: #33cccc;">      </span>.Select(userName)</pre>
<p>What does this all mean?  You just lost 5 minutes of your life.  I hope it was worth it.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/linq-extension-methods-versus-linq-query-language-deathmatch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cannot Resolve Method, Can&#8217;t Infer Return Type, and Funcs</title>
		<link>http://byatool.com/net-issues/funcs-type-inference-and-linq-and-bears-oh-my/</link>
		<comments>http://byatool.com/net-issues/funcs-type-inference-and-linq-and-bears-oh-my/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 20:07:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[.Net Issues]]></category>
		<category><![CDATA[Anonymous Methods]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=79</guid>
		<description><![CDATA[So ran into this today and the answer was actually a lot easier to understand than I thought it would be. Say you want to order a list of objects by a number. Seems simple. Now if you have been paying attention you would know I like using Funcs. Func&#60;SomeClass, Int32&#62; orderByNumber = currentClass =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>So ran into this today and the answer was actually a lot easier to understand than I thought it would be.</p>
<p>Say you want to order a list of objects by a number. Seems simple. Now if you have been paying attention you would know I like using Funcs.</p>
<pre>  <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">SomeClass</span>, <span style="color: #33cccc;">Int32</span>&gt; orderByNumber =
    currentClass =&gt;  currentClass.SomeNumber;

  anotherCollection = someCollection.OrderBy(orderByNumber);</pre>
<p>Seems simple, but what if you wanted to use a method already defined in the class?</p>
<pre>  <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">Int32</span> ReturnNumber(<span style="color: #33cccc;">SomeClass</span> currentClass)
  {
    <span style="color: #0000ff;">return</span> currentClass.SomeNumber;
  }</pre>
<p>It seems like this could be the way to go, right?</p>
<pre>  someCollection.OrderBy(ReturnNumber);</pre>
<p>Compile and BOOOOOOM you get an error. It says it can&#8217;t infer the return type of the method. Wait what? It&#8217;s pretty obvious right, it&#8217;s an integer. It had no problem inferring from the <span style="color: #33cccc;">Func</span> and you have to figure that the method itself is &#8220;typed&#8221; also. Well here&#8217;s the problem (And you&#8217;re dumb for not knowing this, but I&#8217;m not because I&#8217;m immune to dumb), ReturnNumber isn&#8217;t a method, it&#8217;s part of a method group. You can have a million (well maybe not that many) methods named ReturnNumber, all with different parameters. Why is this a problem? Well let&#8217;s use lambda expressions:</p>
<pre>  someCollection.OrderBy(currentClass =&gt;  currentClass.SomeNumber);</pre>
<p>At this point it knows two things: currentClass is a <span style="color: #33cccc;">SomeClass</span> and there is a Method that takes in a <span style="color: #33cccc;">SomeClass</span> and returns something. So with that in mind, it looks for such a method and finds the return type. This is no different with the <span style="color: #33cccc;">Func</span> since the <span style="color: #33cccc;">Func</span> is basically unique due to it being a named field. After all you can&#8217;t have two fields named orderByNumber, but you can have many methods named ReturnNumber. That is where the problem is. When you use the second example:</p>
<pre>  someCollection.OrderBy(ReturnNumber);</pre>
<p>It can infer the <span style="color: #33cccc;">SomeClass</span> from the list and it sees the method. For there it has to find the method&#8217;s return type. Wait, which method? If i Have 10 overloads, each with different return types, how does it know what type to use?  Well the answers is, it doesn&#8217;t.  So basically you&#8217;re screwed.  Sucks, huh?</p>
<p>Side note: This works</p>
<pre>  <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">SomeClass</span>, <span style="color: #33cccc;">Int32</span>&gt; orderByNumber = ReturnNumber;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/net-issues/funcs-type-inference-and-linq-and-bears-oh-my/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uhg It Won&#8217;t End</title>
		<link>http://byatool.com/general-coding/uhg-it-wont-end/</link>
		<comments>http://byatool.com/general-coding/uhg-it-wont-end/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 17:22:58 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=60</guid>
		<description><![CDATA[Still on the readability thing, but there was a second argument in the post that inspired now what is three posts of my own here. The question was should you use Linq based on people saying it&#8217;s more readable, therefore just making it syntax sugar. foreach(Item current in itemList) { itemNameList.Add(current.Name); } Versus var itemNameList [...]]]></description>
			<content:encoded><![CDATA[<p>Still on the readability thing, but there was a second argument in the <a title="HERHEHREHRE" href="http://keithelder.net/blog/archive/2008/10/08/Balancing-Readability-and-New-Syntax-Sugar-in-C-3.0.aspx">post</a> that inspired now what is three posts of my own here. The question was should you use Linq based on people saying it&#8217;s more readable, therefore just making it syntax sugar.</p>
<pre>  <span style="color: #0000ff;">foreach</span>(<span style="color: #33cccc;">Item</span> current <span style="color: #0000ff;">in</span> itemList)
  {
     itemNameList.Add(current.Name);
  }</pre>
<p>Versus</p>
<pre> <span style="color: #0000ff;">var</span> itemNameList = <span style="color: #0000ff;">from</span> item <span style="color: #0000ff;">in</span> itemList
                    <span style="color: #0000ff;">select</span> item.Name;</pre>
<p>Or</p>
<pre>  <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">Item</span>, <span style="color: #33cccc;">String</span>&gt; itemName = current =&gt; current.Name;
  itemNameList.Select(itemName);</pre>
<p>So at this point it&#8217;s really a matter of preference. Problem is, you have to look closer to why the third is so much more than syntax yummies.</p>
<p>Say you want a method that takes in a UserList and you want to select all the users that have a property (Could be name, address, whatever) that matches a string. Well you could do this:</p>
<pre> <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">IList</span>&lt;<span style="color: #33cccc;">User</span>&gt; AllUsersThatMatch(<span style="color: #33cccc;">IList</span>&lt;<span style="color: #33cccc;">User</span>&gt; userList, <span style="color: #33cccc;">NeededProperty </span>property, <span style="color: #33cccc;">String </span>value)
 {
    <span style="color: #33cccc;">IList</span>&lt;<span style="color: #33cccc;">User</span>&gt; returnList;

    returnList = <span style="color: #0000ff;">new</span> List();

    <span style="color: #0000ff;">foreach</span>(UserItem currentUser <span style="color: #0000ff;">in</span> userList)
    {
        <span style="color: #0000ff;">switch</span>(property)
        {
            <span style="color: #0000ff;">case</span>(NeededProperty.Name):
                <span style="color: #0000ff;">if</span>(currentUser.Name == value)
                {
                    userList.Add(currentUser);
                }
                <span style="color: #0000ff;">break</span>;
            <span style="color: #0000ff;">case</span>(NeededProperty.Phone):
                <span style="color: #0000ff;">if</span>(currentUser.Phone == value)
                {
                    userList.Add(currentUser);
                }
                <span style="color: #0000ff;">break</span>;
        }
    }
 }</pre>
<p>Or you could do this:</p>
<pre> <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">User</span>, <span style="color: #33cccc;">Boolean</span>&gt; MatchesProperty(<span style="color: #33cccc;">NeededProperty </span>property, <span style="color: #33cccc;">String</span> value)
 {
    <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">User</span>, <span style="color: #33cccc;">Boolean</span>&gt; returnValue;

    <span style="color: #0000ff;">switch</span>(property)
    {
        <span style="color: #0000ff;">case</span> <span style="color: #33cccc;">NeededProperty</span>.Name:
            returnValue = currentItem =&gt; currentItem.Name == value;
            <span style="color: #0000ff;">break</span>;
        <span style="color: #0000ff;">case</span> <span style="color: #33cccc;">NeededProperty</span>.Phone:
            returnValue = currentItem =&gt; currentItem.Phone == value;
            <span style="color: #0000ff;">break</span>;
    }
    <span style="color: #0000ff;">return</span> returnValue;
  }

 <span style="color: #0000ff;">public</span> <span style="color: #33cccc;">IList</span>&lt;<span style="color: #33cccc;">User</span>&gt; AllUsersThatMatch(<span style="color: #33cccc;">IList</span>&lt;<span style="color: #33cccc;">User</span>&gt; userList, <span style="color: #33cccc;">NeededProperty </span>property, <span style="color: #33cccc;">String </span>value)
 {
    <span style="color: #33cccc;">IList</span>&lt;<span style="color: #33cccc;">User</span>&gt;  returnList;

    returnList = userList.Where(MatchesProperty(property, value));
    <span style="color: #0000ff;">return</span> returnList;
 }</pre>
<p>Now which do you think is easier to upkeep? For those of you wondering what I did, I simply used a method that would return the Func I needed for the passed in Enum and called it in the Where clause. The amount of code is probably close to the same right now, but add in 5 more values for the NeededProperty enum and you&#8217;ll see the code amount differing more and more.</p>
<p>I realize this isn&#8217;t the best of example, and probably the first way could be refactored but the idea is still there. The Linq Method approach gives you a lot more flexibility in the long run with dynamic stuff like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/uhg-it-wont-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Is Readable Addon</title>
		<link>http://byatool.com/pontification/what-is-readable-addon/</link>
		<comments>http://byatool.com/pontification/what-is-readable-addon/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 15:03:43 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Pontification]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=58</guid>
		<description><![CDATA[Quick thought too about which to use due to readability: var you = from factor in seansAwesomeness select new FactorLite { Amount = amount; }; or you could do: Func&#60;Person, FactorLite&#62; selectFactorLite = currentFactor =&#62; new FactorLite { Amount = currentFactor.Amount }; seansAwesomeness.Select(selectFactorLite); I guess it&#8217;s a matter of preference, but the first seems way [...]]]></description>
			<content:encoded><![CDATA[<p>Quick thought too about which to use due to readability:</p>
<pre><span style="color: #0000ff;">var</span> you = <span style="color: #0000ff;">from</span> factor <span style="color: #0000ff;">in</span> seansAwesomeness
          <span style="color: #0000ff;">select</span> <span style="color: #0000ff;">new</span> FactorLite
          {
             Amount = amount;
          };</pre>
<p>or you could do:</p>
<pre><span style="color: #00ccff;">Func</span>&lt;<span style="color: #00ccff;">Person</span>, <span style="color: #00ccff;">FactorLite</span>&gt; selectFactorLite = currentFactor =&gt; <span style="color: #0000ff;">new</span> <span style="color: #00ccff;">FactorLite </span>{ Amount = currentFactor.Amount };

seansAwesomeness.Select(selectFactorLite);</pre>
<p>I guess it&#8217;s a matter of preference, but the first seems way too verbose for something too simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/pontification/what-is-readable-addon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Is Readable</title>
		<link>http://byatool.com/pontification/what-is-readable/</link>
		<comments>http://byatool.com/pontification/what-is-readable/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 14:51:57 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Pontification]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FizzBuzz]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=56</guid>
		<description><![CDATA[So a couple of posts I read recently have been about readability of Linq, more so Linq query expressions versus the Linq methods. Don&#8217;t know what I mean? Expression: var result = from knowledge in Sean select knowledge.Linq; As opposed to: var result = Sean.Select(knowledge =&#62; knowledge.Linq); Personally I would replace the lambda expression with [...]]]></description>
			<content:encoded><![CDATA[<p>So a couple of posts I read recently have been about readability of Linq, more so Linq query expressions versus the Linq methods.  Don&#8217;t know what I mean?</p>
<p>Expression:</p>
<pre><span style="color: #0000ff;">var</span> result = <span style="color: #0000ff;">from</span> knowledge <span style="color: #0000ff;">in</span> Sean
             <span style="color: #0000ff;">select</span> knowledge.Linq;</pre>
<p>As opposed to:</p>
<pre><span style="color: #0000ff;">var</span> result = Sean.Select(knowledge =&gt; knowledge.Linq);</pre>
<p>Personally I would replace the lambda expression with a Func, but I can live with it right now. Anywho, the argument is that the first looks better than the second. I really don&#8217;t see this as a looks problem, but a useage problem. Fact is, they both have their uses and you should know how to read both. Why is that? Well here&#8217;s an example CAUSE I KNOW YOU WANT ONE!</p>
<p>One of my earlier posts had to do with <a title="FizzBuzz" href="http://byatool.com/?p=46">solving the FizzBuzz</a> thing with Linq where I gave you this bad ass solution:</p>
<pre> <span style="color: #0000ff;">var</span> result =
      listToConvert
      .<span style="color: #000000;">Where</span>(WhereBothDivisible(fizzNumber, buzzNumber))
      .<span style="color: #000000;">Select</span>(selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>))
      .Concat(
            listToConvert
            .Where(WhereBuzzDivisable(fizzNumber, buzzNumber))
            .Select(selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>)))
            .Concat(
                  listToConvert
                  .Where(WhereFizzDivisable(fizzNumber, buzzNumber))
                  .Select(selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>)))
                  .Concat(
                         listToConvert
                        .Where(WhereNeitherDivisable(fizzNumber, buzzNumber))
                        .Select(selectKeyValuePair(<span style="color: #ff0000;">"Nothing"</span>)));</pre>
<p>As you can see, I&#8217;ve used both Func fields and methods to return Funcs to clean up how this would look. I&#8217;ll even show what it would look like without this approach:</p>
<pre><span style="color: #0000ff;">var</span> result = listToConvert.Where(currentItem =&gt;
             IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber)
             ).Select(currentItem =&gt; new KeyValuePair(currentItem, <span style="color: #ff0000;">"FizzBuzz"</span>)).Concat(...</pre>
<p>Now I can totally admit that this second one I am showing is just ouch. So the first lesson to be learn is that Funcs and Methods that return Funcs can significantly clean up the Linq Method approach.</p>
<p>Now you could do the same with expressions:</p>
<pre> <span style="color: #0000ff;">var</span> fizzBuzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>);

 <span style="color: #0000ff;">var</span> buzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
            <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
            <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>);

 <span style="color: #0000ff;">var</span> fizz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
            <span style="color: #0000ff;">where</span> WhereFizzDivisable(fizzNumber, buzzNumber)
            <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>);

<span style="color: #0000ff;">var</span> neither = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
              <span style="color: #0000ff;">where</span> WhereNeitherDivisable(fizzNumber, buzzNumber)
              <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>);</pre>
<p>Ok so nice and pretty, but now what? Concatenation. This is where is gets ugly:</p>
<pre>  fizzBuzz.Concat(fizz.Concat(buzz.Concat(neither))));</pre>
<p>OR</p>
<pre> <span style="color: #0000ff;">var</span> fizzBuzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>)
                .Concat(
                     <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                     <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                     <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>))
                     .Concat(....);</pre>
<p>See what I&#8217;m getting at? The non expression one is looking a bit better now or maybe this is a fight to see which is less fugly. Now I admit that this may not be the best FizzBuzz solution, but it gives an example or where the Linq queries can go very wrong.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/pontification/what-is-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combine Lambda Expressions: The And and the Or</title>
		<link>http://byatool.com/general-coding/combining-lambda-expressions/</link>
		<comments>http://byatool.com/general-coding/combining-lambda-expressions/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 20:17:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/09/09/combining-lambda-expressions/</guid>
		<description><![CDATA[Found this post here but wanted to make a really simple example to demonstrate this. The idea is simple, take something like this: currentItem =&#62; currentItem.BooleanMethodOne() &#38;&#38; currentItem.BooleanMethodTwo() but say you only want to have one clause or both. Well you could make three separate expressions, but what if you wanted to add even more [...]]]></description>
			<content:encoded><![CDATA[<p>Found this post <a href="http://www.albahari.com/nutshell/predicatebuilder.html">here</a> but wanted to make a really simple example to demonstrate this.</p>
<p>The idea is simple, take something like this:</p>
<pre>currentItem =&gt; currentItem.BooleanMethodOne() &amp;&amp; currentItem.BooleanMethodTwo()</pre>
<p>but say you only want to have one clause or both. Well you could make three separate expressions, but what if you wanted to add even more later? What if you wanted to mix and match? What if you&#8217;re reading thing because you watched to see what page could possibly be on the last page of a google search? Well I have answers&#8230; stolen answers.</p>
<p>First the needed And and Or methods:</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; And&lt;T&gt;(
<span style="color: #00cccc;">   Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionOne,
<span style="color: #00cccc;">    Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionTwo
)
{
<span style="color: #009900;">  //Basically this is like a bridge between the two expressions.  It will take the T</span>
<span style="color: #009900;">  //parameter from expressionOne and apply it to expression two. So if </span>
<span style="color: #009900;">  // oneItem =&gt; oneItem.OneMethod() is expressionOne</span>
<span style="color: #009900;">  // twoItem =&gt; twoItem.TwoMethod() is expressionTwo</span>
<span style="color: #009900;">  //it would be like replacing the twoItem with the oneItem so that they now point</span>
<span style="color: #009900;">  //to the same thing.</span>
<span style="color: #3333ff;">  var</span> invokedSecond = <span style="color: #00cccc;">Expression</span>.Invoke(expressionTwo, expressionOne.Parameters.Cast&lt;<span style="color: #00cccc;">Expression</span>&gt;());

<span style="color: #009900;">  //Now this is to create the needed expresions to return.  It will take both early expressions</span>
<span style="color: #009900;">  //and use the item from the first expression in both.</span>
<span style="color: #009900;">                    </span>
<span style="color: #009900;">  //It will look something like this:</span>
<span style="color: #009900;">  //currentItem =&gt; (currentItem.OneMethod And Invoke(currentItem =&gt; currentItem.TwoMethod()))</span>
<span style="color: #009900;">  //As you can see, it looks to be running expressionOne and then a new method that basically</span>
<span style="color: #009900;">  //calls expressionTwo with the same value (currentItem)</span>
<span style="color: #3333ff;">  return</span> <span style="color: #00cccc;">Expression</span>.Lambda&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt;(<span style="color: #00cccc;">
   Expression</span>.And(expressionOne.Body, invokedSecond), expressionOne.Parameters
  );
}

<span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Expressio</span><span style="color: #00cccc;">n</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; Or&lt;T&gt;(<span style="color: #00cccc;">
Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionOne, <span style="color: #00cccc;">
Expression</span>&lt;<span style="color: #00cccc;">Fun</span>c&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt; expressionTwo
)
{
<span style="color: #3333ff;">  var</span> invokedSecond = <span style="color: #00cccc;">Expression</span>.Invoke(expressionTwo, expressionOne.Parameters.Cast&lt;<span style="color: #00cccc;">Expression</span>&gt;());

<span style="color: #3333ff;">  return</span> <span style="color: #00cccc;">Expression</span>.Lambda&lt;<span style="color: #00cccc;">Func</span>&lt;T, <span style="color: #00cccc;">Boolean</span>&gt;&gt;(
<span style="color: #00cccc;">   Expression</span>.Or(expressionOne.Body, invokedSecond), expressionOne.Parameters
  );
}</pre>
<p>And here&#8217;s a test for it:</p>
<pre><span style="color: #3333ff;">String</span>[] list;

list = <span style="color: #3333ff;">new</span> <span style="color: #3333ff;">String</span>[] { <span style="color: #990000;">"a"</span>, <span style="color: #990000;">"b"</span>, <span style="color: #990000;">"c"</span>, <span style="color: #990000;">"ac"</span>, <span style="color: #990000;">"ab"</span>, <span style="color: #990000;">"cc"</span>, <span style="color: #990000;">"d"</span>, <span style="color: #990000;">"dd"</span>, <span style="color: #990000;">"dc"</span> };

<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; stringLikeA = currentString =&gt; currentString.Contains(<span style="color: #990000;">"a"</span>);
<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; stringLikeB = currentString =&gt; currentString.Contains(<span style="color: #990000;">"b"</span>);
<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, <span style="color: #00cccc;">Boolean</span>&gt;&gt; stringLikeC = currentString =&gt; currentString.Contains(<span style="color: #990000;">"c"</span>);

<span style="color: #00cccc;">Expression</span>&lt;<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">String</span>, Boolean&gt;&gt; neededUser = And&lt;<span style="color: #00cccc;">String</span>&gt;(stringLikeA, stringLikeB);
list.Where(neededUser.Compile());

<span style="color: #009900;">//a</span>
<span style="color: #00cccc;">Assert</span>.IsTrue(list.Where(neededUser.Compile()).Count() == 1);  //ab

<span style="color: #009900;">//a, c, ac, ab, cc, dc</span>
neededUser = Or&lt;<span style="color: #00cccc;">String</span>&gt;(stringLikeA, stringLikeC);

<span style="color: #00cccc;">Assert</span>.IsTrue(list.Where(neededUser.Compile()).Count() == 6);

<span style="color: #009900;">//ab, c, ac, cc, dc</span>
neededUser = And&lt;<span style="color: #00cccc;">String</span>&gt;(stringLikeA, stringLikeB);
neededUser = Or&lt;<span style="color: #00cccc;">String</span>&gt;(neededUser, stringLikeC);
<span style="color: #00cccc;">Assert</span>.IsTrue(list.Where(neededUser.Compile()).Count() == 5);</pre>
<p>USINGS!!ONEONE</p>
<pre><span style="color: #3333ff;">using</span> System;
<span style="color: #3333ff;">using</span> System.Linq;
<span style="color: #3333ff;">using</span> System.Linq.Expressions;
<span style="color: #3333ff;">using</span> Microsoft.VisualStudio.TestTools.UnitTesting;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/combining-lambda-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solve FizzBuzz Using Linq Extension Methods</title>
		<link>http://byatool.com/general-coding/fizzbuzz-with-linq-extension-methods/</link>
		<comments>http://byatool.com/general-coding/fizzbuzz-with-linq-extension-methods/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 13:40:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FizzBuzz]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/09/08/fizzbuzz-with-linq-extension-methods/</guid>
		<description><![CDATA[So if you haven&#8217;t heard of the FizzBuzz test, it&#8217;s basically taking in a list of numbers and figuring out if they are divisible, cleanly, by two numbers. Say you have 3 and 5 and this is your list: 1 3 5 10 15 If the number is divisible by 3, then return the Fizz [...]]]></description>
			<content:encoded><![CDATA[<p>So if you haven&#8217;t heard of the FizzBuzz test, it&#8217;s basically taking in a list of numbers and figuring out if they are divisible, cleanly, by two numbers. Say you have 3 and 5 and this is your list:</p>
<p>1</p>
<p>3</p>
<p>5</p>
<p>10</p>
<p>15</p>
<p>If the number is divisible by 3, then return the Fizz string. If the number is divisible by 5, return a Buzz string. If it&#8217;s divisible by both, then return FizzBuzz.</p>
<p>1</p>
<p>Fizz</p>
<p>Buzz</p>
<p>Buzz</p>
<p>FizzBuzz</p>
<p>Pretty simple and in actuality pretty easy to do with old C# tools, but I wanted to do this with Linq. With the use of Funcs, Actions, and Linq extension methods it can be done fairly easily. Technically you can do the whole thing in one line if you don&#8217;t want to bother with refactoring.</p>
<p>I have no idea how to format this cleanly, so sorry if the format is confusing. Basically it is take a list, get the ones you want, concatenate it with the next list.</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">IList</span>&lt;<span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">String</span>&gt;&gt; ConvertListOfIntegersWithLinqMethods(<span style="color: #00cccc;">IList</span>&lt;<span style="color: #00cccc;">Int32</span>&gt; listToConvert, <span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
<span style="color: #3333ff;">var</span> result =
  listToConvert
    .Where(WhereBothDivisible(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"FizzBuzz"</span>))
    .Concat(
  listToConvert
    .Where(WhereBuzzDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"Buzz"</span>)))
    .Concat(
  listToConvert
    .Where(WhereFizzDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"Fizz"</span>)))
    .Concat(
  listToConvert
    .Where(WhereNeitherDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"Nothing"</span>)));

 return result.ToList().OrderBy(currentItem =&gt; currentItem.Key).ToList();
}</pre>
<p>Using these:</p>
<pre><span style="color: #3333ff;">private</span> <span style="color: #3333ff;">stati</span>c <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">String</span>&gt;&gt; selectKeyValuePair(<span style="color: #00cccc;">String</span> value)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">String</span>&gt;(currentItem, value);
}

<span style="color: #3333ff;">private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereBothDivisible(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span>  currentItem =&gt; IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber);
}

<span style="color: #3333ff;">private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereFizzDivisable(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; IsDivisible(currentItem, fizzNumber) &amp;&amp; !IsDivisible(currentItem, buzzNumber);
}

<span style="color: #3333ff;">private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereBuzzDivisable(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; !IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber);
}

<span style="color: #3333ff;"> private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereNeitherDivisable(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; !IsDivisible(currentItem, fizzNumber) &amp;&amp; !IsDivisible(currentItem, buzzNumber);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/fizzbuzz-with-linq-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beyond the wall</title>
		<link>http://byatool.com/lessons/beyond-the-wall/</link>
		<comments>http://byatool.com/lessons/beyond-the-wall/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 13:41:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/09/02/beyond-the-wall/</guid>
		<description><![CDATA[So I never gave a solution to this problem and thought I might do that real fast. If you recall, this was the main sticking point of creating a Func for a select clause: Func&#60;User, EHH??&#62; selectUserID = currentUser =&#62; new { currentUser.ID, currentUser.UserName }; Well there is no one solution to this, but there [...]]]></description>
			<content:encoded><![CDATA[<p>So I never gave a solution to<a href="http://byatool.blogspot.com/2008/08/and-then-you-hit-wall.html"> this problem</a> and thought I might do that real fast.</p>
<p>If you recall, this was the main sticking point of creating a Func for a select clause:</p>
<pre><span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">EHH??</span>&gt; selectUserID = currentUser =&gt;  <span style="color: #3333ff;">new</span> { currentUser.ID, currentUser.UserName };</pre>
<p>Well there is no one solution to this, but there is an easy and clean solution:</p>
<pre> <span style="color: #3333ff;">public</span> <span style="color: #3333ff;">class</span> UserQueryItem
 {
   <span style="color: #3333ff;">public</span> UserQueryItem ( <span style="color: #00cccc;">Int32</span> userID, <span style="color: #00cccc;">String</span> userName )
   {
      UserID = userID;
      UserName = userName;
   }

   <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">Int32</span> UserID { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
   <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">String</span> UserName { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
 }</pre>
<p>Create a class to hold the information.</p>
<pre> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">UserQueryItem</span>&gt; selectUserID = currentUser =&gt;  <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">UserQueryItem</span> { UserID = currentUser.ID, UserName = currentUser.UserName };</pre>
<p>Or</p>
<pre>
 <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">User</span>, <span style="color: #00cccc;">UserQueryItem</span>&gt; selectUserID = currentUser =&gt;  <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">UserQueryItem</span> (currentUser.ID, currentUser.UserName);</pre>
<p>Pretty simple, just a little more work.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/beyond-the-wall/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>
		<item>
		<title>Life is fun when you are slow</title>
		<link>http://byatool.com/general-coding/life-is-fun-when-you-are-slow/</link>
		<comments>http://byatool.com/general-coding/life-is-fun-when-you-are-slow/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 20:29:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/08/19/life-is-fun-when-you-are-slow/</guid>
		<description><![CDATA[public void IfTrueRunMethod(Func&#60;Boolean&#62; trueMethod, Action action) { if(trueMethod()) { action(); } } Just something I made for the hell of it to remove: if(someClass != null &#38;&#38; someClass.Property == "hi") { SomeMethod(); } This can be reduced to one line&#8230; yay! IfTrueRunMethod(() =&#62; { someClass != null &#38;&#38; someClass.Property == "hi" }, () =&#62; SomeMethod()); [...]]]></description>
			<content:encoded><![CDATA[<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">void</span> IfTrueRunMethod(<span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Boolean</span>&gt; trueMethod, <span style="color: #00cccc;">Action</span> action)
{
  <span style="color: #3333ff;">if</span>(trueMethod())
  {
    action();
  }
}</pre>
<p>Just something I made for the hell of it to remove:</p>
<pre><span style="color: #3333ff;">if</span>(someClass != <span style="color: #3333ff;">null</span> &amp;&amp; someClass.Property == <span style="color: #cc0000;">"hi"</span>)
{
  SomeMethod();
}</pre>
<p>This can be reduced to one line&#8230; yay!</p>
<pre>IfTrueRunMethod(() =&gt; { someClass != <span style="color: #3333ff;">null</span> &amp;&amp; someClass.Property == <span style="color: #cc0000;">"hi"</span> }, () =&gt; SomeMethod());</pre>
<p>You could even transform the first part into a method if you want:</p>
<pre>IfTrueRunMethod(() =&gt; TrueMethod(someClass), () =&gt; SomeMethod());</pre>
<p>Weeeee!</p>
<pre><span style="color: #3333ff;">using</span> System;
<span style="color: #3333ff;">using</span> System.Linq;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/life-is-fun-when-you-are-slow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I ain&#8217;t gettin&#8217; paid enough for this&#8230;</title>
		<link>http://byatool.com/lessons/i-aint-gettin-paid-enough-for-this/</link>
		<comments>http://byatool.com/lessons/i-aint-gettin-paid-enough-for-this/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 20:55:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Closures]]></category>
		<category><![CDATA[Func]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/14/i-aint-gettin-paid-enough-for-this/</guid>
		<description><![CDATA[Remember when I said I&#8217;m not exactly great with programming terms? Well if I didn&#8217;t, I am saying it now. So I saw the word &#8220;closures&#8221; today and had no idea what this was. All excited about something new, I found out that closures is just another word for anonymous methods, although it may include [...]]]></description>
			<content:encoded><![CDATA[<p>Remember when I said I&#8217;m not exactly great with programming terms? Well if I didn&#8217;t, I am saying it now. So I saw the word &#8220;closures&#8221; today and had no idea what this was. All excited about something new, I found out that closures is just another word for anonymous methods, although it may include stuff outside that scope but I&#8217;m not sure. Anyhow, although this seemed to be yet another attack of the stupid I stumbled across something <a href="http://blogs.msdn.com/abhinaba/archive/2005/08/08/448939.aspx">here.</a> Basically what caught me is how value types are handled with Funcs. Take this method:</p>
<pre><span style="color: #3333ff;">using</span> System;
<span style="color: #3333ff;">using</span> System.Collections.Generic;
<span style="color: #3333ff;">using</span> System.Linq;

<span style="color: #3333ff;">public static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>&gt; ReturnIntegerWithinFuncReturningMethod(<span style="color: #00cccc;">WhichMethod</span> methodToReturn)
{
<span style="color: #3333ff;">  <span style="color: #00cccc;">Int32</span></span> integerToIncrement;
<span style="color: #3333ff;">  <span style="color: #00cccc;">Func</span></span>&lt;<span style="color: #00cccc;">Int32</span>&gt; returnMethod;

<span><span style="color: #3333ff;">  </span></span>integerToIncrement = 0;
<span><span style="color: #3333ff;">  </span></span>returnMethod = <span style="color: #3333ff;">null</span>;

<span><span style="color: #3333ff;">  </span></span><span style="color: #3333ff;">switch</span>(methodToReturn)
<span><span style="color: #3333ff;">  </span></span>{
<span style="color: #3333ff;">  case</span> <span style="color: #00cccc;">WhichMethod</span>.AddOne:
   returnMethod = <span style="color: #3333ff;">new</span> <span style="color: #3333ff;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>&gt;(() =&gt; { <span style="color: #3333ff;">return</span> integerToIncrement += 1;} );
 <span style="color: #3333ff;">  break</span>;
<span style="color: #3333ff;">  case</span> <span style="color: #00cccc;">WhichMethod</span>.AddTwo:
   returnMethod = <span style="color: #3333ff;">new Func</span>&lt;<span style="color: #00cccc;">Int32</span>&gt;(() =&gt; { <span style="color: #3333ff;">return</span> integerToIncrement += 2; });
 <span style="color: #3333ff;">  break</span>;
}

<span style="color: #3333ff;"> return</span> returnMethod;
}</pre>
<p>Let&#8217;s say we test it like this:</p>
<pre><span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>&gt; returnedMethod;
<span style="color: #00cccc;">Int32</span> integerToCheck;

returnedMethod = ReturnIntegerWithinFuncReturningMethod(<span style="color: #00cccc;">WhichMethod</span>.AddOne);
integerToCheck = returnedMethod();
<span style="color: #00cccc;">Assert</span>.IsTrue(integerToCheck == 1, <span style="color: #cc0000;">"Actual value is:"</span> + integerToCheck.ToString());

integerToCheck = returnedMethod();
<span style="color: #00cccc;">Assert</span>.IsTrue(integerToCheck == 2, <span style="color: #cc0000;">"Actual value is:"</span> + integerToCheck.ToString());</pre>
<p>These both pass. How does that make sense? You would think that it would return 1 both times since from first glance integerToIncrement lives outside the actual <span style="color: #000000;">Func</span> that is returned. You would think from this that Int32 is a reference type. When a value type is &#8220;attached&#8221; to a Func like this one is, apparently it keeps a reference to it and therefore the original Int32 is updated every time the Func is called. Something to remember.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/i-aint-gettin-paid-enough-for-this/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

