<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>DotNet - Kennydust</title>
        <description>Thoughts from a C# Developer</description>
        <link>/site.aspx/Tags/DotNet/RSS</link>
        <language>en</language>
        <image>
            <url>http://www.kennydust.com/Content/icons/flame.png</url>
            <title>Kennydust</title>
            <link>/site.aspx/Tags/DotNet/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>kennydust</dc:creator>
            <title>Lambda Expressions</title>
            <description>&lt;p&gt;This just in: &lt;a href=&quot;http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx&quot; target=&quot;_blank&quot;&gt;Lambda Expressions&lt;/a&gt; are cool.&lt;/p&gt;  &lt;p&gt;Yes, I'm a tad late, given that this language feature has been around for some time (introduced originally in .net 3.0, and packaged in .net 3.5). But to my defense, I have these reasons: (1) There wasn’t a need for it on my end, (2) I was working in a .net 2.0 shop, (3) There were so many feature enhancements that this particular one flew under the radar.&lt;/p&gt;  &lt;p&gt;Fast forward to today, I've written my first lambda expression and wish to share it.&lt;/p&gt;  
&lt;p&gt;So what are lambda expressions? In my own words, they are just another way of writing code statements; a shorthand way of encapsulating logic. They can be &quot;expressions&quot; and &quot;statements&quot;, and can be assigned to a delegate type. A lambda expression contains a &quot;lambda operator,&quot; =&gt;, with the left side of the expression responsible for handling input and the right side containing logic:&lt;/p&gt;

&lt;pre class=&quot;brush: c-sharp;&quot;&gt;
  //assuming x equates to a value of 10
  x =&gt; x + x;

  (10) =&gt; (10) + (10) //equals 20
&lt;/pre&gt;

&lt;p&gt; Below are a few examples I've put together, they are pretty simplified, but it demonstrates how powerful lambda expressions can be. The challenge? Given some DateTime value, perform a comparison to see whether or not this value has exceeded 24 hours.&lt;/p&gt;  

&lt;p&gt;To start, here’s the original snippet that was coded up.&lt;/p&gt;  

&lt;pre class=&quot;brush: c-sharp;&quot;&gt;
DateTime expiredTime = DateTime.Parse(&quot;1/1/2009&quot;); 
DateTime adjustedTime = DateTime.Now.AddHours(-24);

//original statement 
int comparsionResult = DateTime.Compare(adjustedTime, expiredTime); 
bool isExpired = (comparsionResult &amp;gt; 0);
&lt;/pre&gt;

&lt;p&gt;It's pretty straight forward and it does the job. But we want lambda expressions. Here's an example of that, with an expression that's assigned to a delegate type that expects one input:&lt;/p&gt;

&lt;pre class=&quot;brush: c-sharp;&quot;&gt;
delegate bool del(DateTime t); 
static void Main(string[] args)
{
   DateTime expiredTime = DateTime.Parse(&quot;1/1/2009&quot;); 
   DateTime adjustedTime = DateTime.Now.AddHours(-24);

   del comparisonDel = x =&gt; (DateTime.Compare(adjustedTime, x) &gt; 0);
   bool isExpired = comparisonDel(expiredTime);
}
&lt;/pre&gt;


&lt;p&gt;The second example demostrates wrapping a lambda expression in a Func&lt;&gt; generic delegate.&lt;/p&gt;
&lt;pre class=&quot;brush: c-sharp;&quot;&gt;
DateTime expiredTime = DateTime.Parse(&quot;1/1/2009&quot;); 
DateTime adjustedTime = DateTime.Now.AddHours(-24);

 //Lambda Expression Wrapped in a Func&lt;&gt;
Func&lt;DateTime, bool&gt; ComparisonCheck = x =&gt; (DateTime.Compare(adjustedTime, x) &gt; 0);
bool isExpired = ComparisonCheck(expiredTime);
&lt;/pre&gt;</description>
            <link>http://www.kennydust.com/site.aspx/Blog/Lambda-Expressions</link>
            <guid isPermaLink="true">http://www.kennydust.com/site.aspx/Blog/Lambda-Expressions</guid>
            <pubDate>Wed, 05 Aug 2009 15:11:00 GMT</pubDate>
            <category>Code</category>
            <category>CSharp</category>
            <category>DotNet</category>
        </item>
    </channel>
</rss>

