<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="html">DotNet - Kennydust</title>
  <icon>http://www.kennydust.com/Content/icons/flame.ico</icon>
  <logo>http://www.kennydust.com/Content/icons/flame.png</logo>
  <updated>2009-08-05T15:11:00</updated>
  <subtitle type="html">Thoughts from a C# Developer</subtitle>
  <id>http://www.kennydust.com/site.aspx/tags/dotnet/atom</id>
  <link rel="alternate" type="text/html" hreflang="en" href="/site.aspx/tags/dotnet/atom"/>
  <link rel="self" type="application/atom+xml" href="http://www.kennydust.com/site.aspx/Tags/DotNet/ATOM"/>
  <generator uri="http://oxite.net" version="1.0">Oxite</generator>
  <entry>
    <title type="html">Lambda Expressions</title>
    <link rel="alternate" type="text/html" href="http://www.kennydust.com/site.aspx/Blog/Lambda-Expressions"/>
    <id>http://www.kennydust.com/site.aspx/Blog/Lambda-Expressions</id>
    <updated>2009-08-06T21:17:23.75</updated>
    <published>2009-08-05T15:11:00</published>
    <author>
      <name>kennydust</name>
    </author>
    <category term="Code" />
    <category term="CSharp" />
    <category term="DotNet" />
    <content type="html" xml:lang="en">
      &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;
    </content>
  </entry>
</feed>

