<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>LINQ</title>
        <link>http://geekswithblogs.net/dotnetnomad/category/7214.aspx</link>
        <description>Posts on Microsoft's Language INtegrated Query technology.</description>
        <language>en-US</language>
        <copyright>newman</copyright>
        <managingEditor>newman.de@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>LINQ Overview, part two (Lambda Expressions)</title>
            <link>http://geekswithblogs.net/dotnetnomad/archive/2008/01/29/119037.aspx</link>
            <description>&lt;h5&gt;Back Links&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/dotnetnomad/archive/2007/11/09/116739.aspx"&gt;LINQ Overview, part zero&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/dotnetnomad/archive/2007/11/12/116793.aspx"&gt;LINQ Overview, part one (Extension Methods)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;em&gt;NOTE: This article is dedicated to Keith Elder...even if he never sent me a bologna sandwich.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Apparently, two months is my definition of "very soon".  Let's continue. &lt;/p&gt;  &lt;p&gt;Since .NET 1.1 we've had the concept of delegates.  They are the constructs that allow us to call methods on objects via reference such as:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;AddFunc&lt;/span&gt;(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; x, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; y);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;MathOps
&lt;/span&gt;{

&lt;span style="color: rgb(0,0,255)"&gt;   public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; Add(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; x, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; y)
   {

      &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; x + y;

   }&lt;/pre&gt;

&lt;pre class="code"&gt;} &lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Program
&lt;/span&gt;{&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;   static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
   {
           &lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;      AddFunc&lt;/span&gt; f = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;AddFunc&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;MathOps&lt;/span&gt;.Add);

      &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Delegate: 2 + 2 = {0}"&lt;/span&gt;, f(2, 2));&lt;/pre&gt;

&lt;pre class="code"&gt;      &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.ReadLine();

   }&lt;/pre&gt;

&lt;pre class="code"&gt;}&lt;/pre&gt;

&lt;p&gt;There is nothing new and exciting about delegates as calling a function via pointer has been around for a very long time.  In fact, delegates are actually somewhat annoying in terms of syntax.  They must be declared in a class, you must wrap them in an object, etc.  Why can't we have a simpler syntax? After all, most of the time delegates are used to respond to relatively simple events or act as part of a strategy pattern (e.g. in a sort).&lt;/p&gt;

&lt;h3&gt;Anonymous Methods&lt;/h3&gt;

&lt;p&gt;In honor of Bill Gates, .NET 2.0 decided to give us a kindler and gentler delegate syntax.  The main method above could easily be rewritten as:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Program
&lt;/span&gt;{&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;   static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
   {
           &lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;      AddFunc&lt;/span&gt; f = &lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt;(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; x, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; y) { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; x + y; };

      &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"Anonymous Method: 2 + 2 = {0}"&lt;/span&gt;, f(2, 2));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;      &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.ReadLine();

   }&lt;/pre&gt;

&lt;pre class="code"&gt;}&lt;/pre&gt;

&lt;p&gt;As is common on the .NET platform the delegate keyword was overloaded to give it additional meaning.  Now one could assign to a delegate variable directly, in the current scope.  The new anonymous method syntax was similar to a method declaration.  The differences are pretty obvious, but I'll list the major ones.  Firstly, anonymous methods don't require identifiers, hence the terms &lt;em&gt;anonymous&lt;/em&gt; methods.  Secondly, anonymous methods do not need to specify a return type.  This is due to some rudimentary type inference built into the compiler.  In essence, if we already know that we are assigning to a delegate of type "AddFunc" whose return type is "int", it should be obvious to the compiler that as long as the return statements in the delegate's body return an "int" then our anonymous delegate matches the signature of "AddFunc".  The counterintuitive aspect of this is that we still have to specify the types of the anonymous method's arguments.  After all, shouldn't the compiler be smart enough to also assume the types of our "x" and "y" based on the delegate type we are assigning to?  It should be, but unfortunately it is not.&lt;/p&gt;

&lt;p&gt;There is something else I want to say about anonymous methods before moving on. This is something I come across all the time and some developers just don't get: anonymous methods allow for lexical closures.  &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;Lexical Closures&lt;/h3&gt;

&lt;p&gt;There is a lot of bickering on the net about "does .NET support 'true' closures?"  Well, based on my understanding and in my opinion, they support lexical closures or at least something close enough that for most practical purposes it doesn't matter.  I'll leave the 100% correct definition to the language lawyers and just give a quick example and some reasons why a lot of developers get caught in the lexical closure trap.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Increment&lt;/span&gt;();&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Main(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt;[] args)
{
   &lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;   Increment&lt;/span&gt; AddOne = AnonInc(0, 1);
   &lt;span style="color: rgb(43,145,175)"&gt;Increment&lt;/span&gt; SubOne = AnonInc(10, -1);

   &lt;span style="color: rgb(0,0,255)"&gt;for&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 10; ++i)
   {

      &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"{0},{1}"&lt;/span&gt;, AddOne(), SubOne());

   }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;   Console&lt;/span&gt;.ReadLine();&lt;/pre&gt;

&lt;pre class="code"&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;Increment&lt;/span&gt; AnonInc(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; start, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; by)
{

&lt;span style="color: rgb(0,0,255)"&gt;   return&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; start = start + by; };

}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The output of the about code should be:&lt;/p&gt;

&lt;pre class="code"&gt;1,9
2,8
3,7
4,6
5,5,
6,4,
7,3
8,2
9,1
10,0&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;First, take a look at our delegate "Increment".  It takes no arguments and returns an "int".  The idea is that delegates will somehow increment "a value" and return the next value in the sequence.  &lt;/p&gt;

&lt;p&gt;Next, look at the method "AnonInc".  Does it return a delegate? That's crazy!  Further, it returns a delegate that makes use of something commonly referred to as "up values" or "outer variables" depending on the person/system/said person's mood.  An outer variable is simply a variable that exists in the scope that contains the delegate.  In this case, our delegate's scope is the "AnonInc" method in which the "start" and "by" arguments are implicitly defined local variables.  &lt;/p&gt;

&lt;p&gt;Now, based on the definition of the delegate returned by "AnonInc" and the output of the program we can tell something interesting is going on here.  The question you should be asking right now is, "How is it that we are modifying the value of a local variable inside a delegate and it is keeping track of the change?"&lt;/p&gt;

&lt;p&gt;If you recall delegates, and therefore anonymous methods, are represented by objects.  These objects are instances of classes that are automatically generated for you at compile time.  They have funny, mangled names and you can not really do too much with them.  The thing that one needs to know is that any outer variables used by an anonymous delegate become attributes of this auto-generated class.  So, in our case if we look at the assembly generated by the above program using a tool like Reflector we should find a class like:&lt;/p&gt;

&lt;pre class="code"&gt;[CompilerGenerated]
&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;sealed&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &amp;lt;&amp;gt;c__DisplayClass7
{
    &lt;span style="color: rgb(0,128,0)"&gt;// Fields
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; by;
    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; start;

    &lt;span style="color: rgb(0,128,0)"&gt;// Methods
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; &amp;lt;AnonInc&amp;gt;b__6()
    {
        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.start += &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.by);
    }
}&lt;/pre&gt;

&lt;p&gt;As you can see, the above class has two attributes with the same names as our outer variables and a method that accesses them.  Looking at the code this way kind of takes the magic out of anonymous methods and we being to realize that it is sort of like what I said about extension methods, it is just syntactic sugar.  Handy, but not magical.&lt;/p&gt;

&lt;p&gt;So, what is this trap I was talking about?  Well, it has to do with the garbage collector.  As we all know, in .NET an object lives in memory until it is explicitly disposed of or goes out of scope.  In general perhaps "goes out of scope" is best thought of as "until no other object holds a reference to it".  With lexical closures happening more or less behind the scenes it is very easy to create a memory leak such as the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ResourceWrapper
&lt;/span&gt;{

    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; OpenOnClick(&lt;span style="color: rgb(43,145,175)"&gt;Button&lt;/span&gt; btnOpen, &lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; resourcePath)
    {

        &lt;span style="color: rgb(43,145,175)"&gt;SomeResource&lt;/span&gt; res = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;SomeResource&lt;/span&gt;(resourcePath);

        btnOpen.Click += &lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt;(&lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43,145,175)"&gt;EventArgs&lt;/span&gt; e) { res.Access(); };

    }
    
}

&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;SomeResource
&lt;/span&gt;{

    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; SomeResource(&lt;span style="color: rgb(0,0,255)"&gt;string&lt;/span&gt; path) { }

    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Access() { }

}&lt;/pre&gt;

&lt;p&gt;Granted, this example is contrived, but you see similar things all the time.  So, what's going on here? Basically if we look at "OpenOnClick" we can see that an anonymous method is being registered as the Click event for a button.  Further, the anonymous method is using an outer variable "res".  This means that the following class gets generated for us:&lt;/p&gt;

&lt;pre class="code"&gt;[CompilerGenerated]
&lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;sealed&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &amp;lt;&amp;gt;c__DisplayClass1
{
    &lt;span style="color: rgb(0,128,0)"&gt;// Fields
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; SomeResource res;

    &lt;span style="color: rgb(0,128,0)"&gt;// Methods
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; &amp;lt;OpenOnClick&amp;gt;b__0(&lt;span style="color: rgb(0,0,255)"&gt;object&lt;/span&gt; sender, EventArgs e)
    {
        &lt;span style="color: rgb(0,0,255)"&gt;this&lt;/span&gt;.res.Access();
    }
}&lt;/pre&gt;

&lt;p&gt;Normally, we'd just assume that since "res" is a local variable in the "OpenOnClick" method that it'd die as soon as it ran out of scope, i.e. at the end of the method.  However, since our anonymous delegate is holding a reference to it, the object "res" is referencing will live until the anonymous delegate itself goes out of scope.  One can easily see how this kind of situation can go bad quickly.  To avoid this situation, be careful to unregister your anonymous methods when you use them as event handlers!&lt;/p&gt;

&lt;p&gt;Alright, so why did I get into all of this anonymous method stuff if the post is supposed to be about Lambda Expressions? Well, because Lambda Expressions in C# are just an evolutionary step beyond anonymous methods.  Let's chip away at some of the sugar...&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;&lt;/h3&gt;

&lt;h3&gt;&lt;/h3&gt;

&lt;h3&gt;Our first Lambda&lt;/h3&gt;

&lt;p&gt;It is difficult to describe the syntax of a lambda expression since it is very ambiguous and depends on multiple factors.  With that in mind let's look at a quick example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;AddFunc&lt;/span&gt; f = (x, y) =&amp;gt; x + y;&lt;/pre&gt;

&lt;p&gt;The above snippet declares a new AddFunc delegate and assigns a lambda expression to it.  Everything to the right of the = operator is the lambda definition.  &lt;/p&gt;

&lt;p&gt;Some questions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Where is the return type? &lt;/li&gt;

  &lt;li&gt;Where is the identifier? &lt;/li&gt;

  &lt;li&gt;Does (x, y) denote the parameter list? &lt;/li&gt;

  &lt;li&gt;What does the =&amp;gt; do? &lt;/li&gt;

  &lt;li&gt;Why isn't there a return statement? &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Some answers:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Lambda expressions do not need an explicit return type.  Just like with anonymous methods the compiler is smart enough to infer the return type based on the type of delegate it is being assigned to. In this case AddFunc returns an int, and so the lambda implicitly returns and int.  Obviously it is a compiler error if the lambda does not. &lt;/li&gt;

  &lt;li&gt;Lambda expressions are by definition anonymous.  They do not have identifiers. &lt;/li&gt;

  &lt;li&gt;Yes.  Further, you should note that lambda parameters do not need to explicitly state their type.  This, like the return type, is inferred by the compiler based on their order compared to the delegate's parameters list. You can, however, state the types explicitly.  (int x, int y) is a valid lambda expression parameter list. &lt;/li&gt;

  &lt;li&gt;The new =&amp;gt; operator is the start of the expression's body.  Everything after =&amp;gt; defines what the lambda expression &lt;em&gt;does&lt;/em&gt;. &lt;/li&gt;

  &lt;li&gt;Lambda expression don't require an explicit return statement.  When a return isn't provided the return value is assumed to be whatever the lambda expression evaluates to. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, let's take a look at a few other valid ways to write lambda expressions:&lt;/p&gt;

&lt;pre class="code"&gt;(&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; x, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; y) =&amp;gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; x + y; };&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The above is the most explicit way.  We've specified types for the parameters and a real return statement.  Notice how when we use an actual return expression we have to use the { } brackets? This same syntax allows us to create multi-line lambdas and lambdas that declare local variables.&lt;/p&gt;

&lt;pre class="code"&gt;(x, y) =&amp;gt; { &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; x + y; };&lt;/pre&gt;

&lt;p&gt;This one keeps the return statement and just drops the optional types in the parameter list.&lt;/p&gt;

&lt;p&gt;() =&amp;gt; x + y;&lt;/p&gt;

&lt;p&gt;In the above, we've specified a lambda with an empty parameter list.  In this case we are assuming the existence of x and y as outer variables (yes, lambda expressions support lexical closures just like anonymous methods).&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;A Lambda is what you assign it to&lt;/h3&gt;

&lt;p&gt;So far we've seen that lambda expressions are compatible with delegates in the sense that you can assign a lambda directly to a delegate, but there are other interesting uses. Take a second and think about writing a program in a text editor.  To the text editor, or for that matter to the compiler, the lines of code your write are just data.  The compiler doesn't execute your program, it simply translates data from one format to another.  It is natural then to ask, "If I can store a program as data, can I load a program as data at run time and then execute it?" With lambda expressions the answer is yes.&lt;/p&gt;

&lt;p&gt;If we assign a lambda expression to a delegate it becomes a delegate of that type.&lt;/p&gt;

&lt;p&gt;If we assign a lambda expression to an appropriately typed Expression Tree it gets converted at compile time to equivalent Expression objects.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt;&amp;gt; exp = (x, y) =&amp;gt; x + y;&lt;/pre&gt;

&lt;p&gt;This statement simply says, "Convert this lambda expression into an expression tree equivalent to a method that takes two integer parameters and returns the sum as an integer".&lt;/p&gt;

&lt;p&gt;There is no resulting compilation of this tree and no execution of code as a result of this statement.  If at runtime we need to execute the function the tree represents, we must say:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43,145,175)"&gt;Expression&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;, &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt;&amp;gt;&amp;gt; exp = (x, y) =&amp;gt; x + y;
&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; func = exp.Compile();
&lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"{0}"&lt;/span&gt;, func(1, 1));&lt;/pre&gt;

&lt;p&gt;Now, it isn't inherently obvious why this is cool so I'll spell it out: If the compiler can represent executable code using Expression objects, so can we.  In fact, we will do exactly that by the end of this series.&lt;/p&gt;

&lt;p&gt;As funny as it may sound, this is all you really need to know about lambda expressions.   You can use them in place of anonymous delegates (and you should), they forced the .NET team to provide C# with something approaching real type inference, and they allow us to represent code as data in a statically type checked way.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;LINQ Tie In&lt;/h2&gt;

&lt;p&gt;Awesome. How are Lambda Expressions useful in LINQ?  Well, by now you've read the basic LINQ syntax somewhere else as I asked so I'll just show a couple of quick examples:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; UseLINQ()
{

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; names = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt;&amp;gt; { 
        &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Bob"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Sally"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Girl }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Jack"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Sarah"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Girl }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Philbert"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy }            
    };

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; boyNames = names.Where((n) =&amp;gt; n.Gender == &lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy).Select((n) =&amp;gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; { n.Name });

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; name &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; boyNames)
        &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"{0}"&lt;/span&gt;, name.Name);

}&lt;/pre&gt;

&lt;p&gt;This above function queries a list of names for those that are traditionally used for boys.  In order to make use of the actual lambda expression syntax I used the method based approach to querying with LINQ.  In fact, there are two lambdas in our code:&lt;/p&gt;

&lt;pre class="code"&gt;(n) =&amp;gt; n.Gender == &lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy&lt;/pre&gt;

&lt;p&gt;This lambda is for our selection criteria and simply compares the given name, n, to see if it is used for boys.  &lt;/p&gt;

&lt;pre class="code"&gt;(n) =&amp;gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; { n.Name }&lt;/pre&gt;

&lt;p&gt;In this expression we are returning a new anonymous type that just contains the Name property of the GenderedName that has passed our selection criteria.&lt;/p&gt;

&lt;p&gt;We can simplify, or rather pretty up, this method by using the new LINQ keywords as so:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;static&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; UseLINQ()
{

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; names = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt;&amp;gt; { 
        &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Bob"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Sally"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Girl }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Jack"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Sarah"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Girl }
        , &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;GenderedName&lt;/span&gt; { Name=&lt;span style="color: rgb(163,21,21)"&gt;"Philbert"&lt;/span&gt;, Gender=&lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy }            
    };

    &lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; boyNames = &lt;span style="color: rgb(0,0,255)"&gt;from&lt;/span&gt; n &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; names
                   &lt;span style="color: rgb(0,0,255)"&gt;where&lt;/span&gt; n.Gender == &lt;span style="color: rgb(43,145,175)"&gt;Gender&lt;/span&gt;.Boy
                   &lt;span style="color: rgb(0,0,255)"&gt;select&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; { n.Name };

    &lt;span style="color: rgb(0,0,255)"&gt;foreach&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;var&lt;/span&gt; name &lt;span style="color: rgb(0,0,255)"&gt;in&lt;/span&gt; boyNames)
        &lt;span style="color: rgb(43,145,175)"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: rgb(163,21,21)"&gt;"{0}"&lt;/span&gt;, name.Name);

}&lt;/pre&gt;

&lt;p&gt;It doesn't look like we are using lambda expressions here, but we really are.  It is just that the compiler needs to turn our pretty code into the same method calls that we just used, and therefore ultimately into an Expression Tree for later execution.&lt;/p&gt;

&lt;p&gt;I just want to be very explicit here and point out something.  When we are using LINQ we use lambda expressions as delegates.  We know this because the parameters of the Where method accept arguments of the Func&amp;lt;T&amp;gt; variety.  The Func series of generic types are actually generic delegates.  For example, MSDN has the following definition for Func&amp;lt;T, TResult&amp;gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;delegate&lt;/span&gt; TResult &lt;span style="color: rgb(43,145,175)"&gt;Func&lt;/span&gt;&amp;lt;T, TResult&amp;gt;(
    T arg
)&lt;/pre&gt;

&lt;p&gt;This usage of delegates and expression trees is what allows LINQ to support Lazy Evaluation.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=119037"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=119037" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/dotnetnomad/aggbug/119037.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>newman</dc:creator>
            <guid>http://geekswithblogs.net/dotnetnomad/archive/2008/01/29/119037.aspx</guid>
            <pubDate>Tue, 29 Jan 2008 15:41:15 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/dotnetnomad/comments/119037.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/dotnetnomad/archive/2008/01/29/119037.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/dotnetnomad/comments/commentRss/119037.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/dotnetnomad/services/trackbacks/119037.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ Overview, part one (Extension Methods)</title>
            <link>http://geekswithblogs.net/dotnetnomad/archive/2007/11/12/116793.aspx</link>
            <description>&lt;h5&gt;Back Links&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/dotnetnomad/archive/2007/11/09/116739.aspx"&gt;LINQ Overview, part zero&lt;/a&gt;&lt;/p&gt;  &lt;h5&gt;Forward Links&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/dotnetnomad/archive/2008/01/29/119037.aspx"&gt;LINQ Overview, part two (Lambda Expressions)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;In part zero I stated my intentions, now it is time to act.&lt;/p&gt;  &lt;p&gt;If you've ever programmed in C (no, I didn't forget the #) you may have had a function prototype laying around similar to:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt; Deposit(&lt;span class="kwrd"&gt;struct&lt;/span&gt; account *acct, &lt;span class="kwrd"&gt;double&lt;/span&gt; amnt);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If one were to rewrite this today in C# you'd probably have a class to represent accounts and your method definition would just be:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Deposit(&lt;span class="kwrd"&gt;double&lt;/span&gt; amount)&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;You would have dropped the int because you can throw an exception if there is an issue and you no longer need to pass in a pointer (i.e. reference) to the account because our account object will contain all the state for us.  In reality what is the difference here? Well, it more or less comes down to syntax.  To execute this code in C I would have to say:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Deposit(&amp;amp;acct, 350.75);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;In C# I'd write:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;acct.Deposit(350.75);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;The great thing about the C# version in terms of syntax is that there are no funky operators to deal with and it is more English like in terms of reading left to right.  In terms of flexibility, however, I have to give the edge to the C version.  Why? Well, because if I need to add a new operation on the account data type in C I can do it anywhere that I want.  All I need to have access to is the prototype of the account struct and I can introduce the following:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt; Withdraw(&lt;span class="kwrd"&gt;struct&lt;/span&gt; account *acct, &lt;span class="kwrd"&gt;double&lt;/span&gt; amnt);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In C#, I simply can't add a "third party" method to a class that I don't have the source code to.  Sure, partial classes allow me to add methods, but they must be in the same namespace and assembly in order to work since they are a compile time construct.  I could also just take the C approach and say something like:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Withdraw(&lt;span class="kwrd"&gt;ref&lt;/span&gt; Account acct, &lt;span class="kwrd"&gt;double&lt;/span&gt; amnt)&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;That however, doesn't clean up the syntax issue from the caller's perspective as they'd now have to pass in the reference to an Account object like so:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Helper.Withdraw(&lt;span class="kwrd"&gt;ref&lt;/span&gt; acct, 350.75);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;Crap! It is actually &lt;strong&gt;&lt;em&gt;more &lt;/em&gt;&lt;/strong&gt;to type than the C version.&lt;/p&gt;

&lt;p&gt;Enter extension methods.  An extension method is a static method whose first parameter is decorated with the overloaded 'this' keyword.  For example:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Withdraw(&lt;span class="kwrd"&gt;this&lt;/span&gt; Account acct, &lt;span class="kwrd"&gt;double&lt;/span&gt; amnt)&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;Alright, still not much shorter for me as the extension method's author, but how does it look when a piece of client code calls it?&lt;/p&gt;

&lt;pre class="csharpcode"&gt;acct.Withdraw(350.75);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;As you can see, the call syntax is exactly the same as with the C# version of Deposit.  Under the hood all that is going on is the compiler is seeing our use of 'this' in front of the first parameter and saying, "OK, I know now that I can allow this to be called on Account objects".  Further, if Account was a base class of another type, e.g. BusinessBankAccount, then the extension method would also work on BusinessBankAccount objects.  Do I need to mention that the same holds true for extensions defined to operate on interfaces?  Heck, you can even make an extension method that makes use of Generics!&lt;/p&gt;

&lt;p&gt;Another cool aspect of extension methods is that in the Visual Studio 2008 (and even Visual Studio 2005 if you have the .NET 3.0 CTP) environment they are fully supported by intellisense.&lt;/p&gt;

&lt;p&gt;There are a couple of restrictions that should be obvious, but I'll list them here anyway.  First, since the extension method is still technically a member of a different class it will only have access to the public members exposed by the class being extended.  In other words, our Withdraw extension method can not access the private and protected members of the Account class.  This restriction places a definite limitation on what can be achieved through extension methods, but is necessary to avoid violating encapsulation.  Second, a consumer of the extension method needs to reference the assembly in which the extension lives or the compiler won't see it.&lt;/p&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;LINQ Tie In&lt;/h2&gt;

&lt;p&gt;Now that we know what extension methods are let's take a look at how they are utilized by LINQ.  LINQ is designed to extend query capability to .NET types using extension methods.  The standard query operators of LINQ operate on any type that implements IEnumerable&amp;lt;T&amp;gt;.  There are other technologies in the LINQ family that provide sets of extensions methods, for example LINQ To Dataset provides the same extensions, but for types derived from Dataset.  &lt;/p&gt;

&lt;p&gt;If we focus for now on vanilla LINQ, i.e. the one that operates on in memory collections, we can think of at least three ways to construct it.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;methods that accept IEnumerable&amp;lt;T&amp;gt; parameters using existing, i.e. pre 3.0, syntax &lt;/li&gt;

  &lt;li&gt;add methods to the IEnumerable&amp;lt;T&amp;gt; interface for things like Select, Join, etc &lt;/li&gt;

  &lt;li&gt;provide extension methods for the IEnumerable&amp;lt;T&amp;gt; interface &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Item one will probably work, but requires the clunky syntax we saw before.  It could possibly be hidden behind new C# keywords, but it may have required more work at the compiler level and would have made doing any type of dynamic LINQ an undue burden on the developer.&lt;/p&gt;

&lt;p&gt;Item two is obviously out the window.  For starters, changing such a core interface like IEnumerable&amp;lt;T&amp;gt; would require so many rewrites not only in the framework, but third party code as well, that Microsoft would have had a developer mutiny on their hands.&lt;/p&gt;

&lt;p&gt;Item three is what they ultimately went with and is basically the same as item one now that we know how extension methods actually work.  The advantage is the cleaner syntax offered to developers.&lt;/p&gt;

&lt;h2&gt;Naive, LINQ-like Extension&lt;/h2&gt;

&lt;p&gt;There are probably a million blogs out there that have the information you've already seen here so far.  So, I am not going to go over the actual LINQ syntax right now.  Instead I am going to show the method by which LINQ is constructed in a very limited, naive case.  &lt;/p&gt;

&lt;p&gt;Let's say LINQ doesn't exist and your team was on a project where you were constantly searching through collections of objects using lots of basic criteria.  We could introduce an extension to the IEnumerable&amp;lt;T&amp;gt; type that allows us to specify our criteria and get back a new collection of items that match it.  Our method might look something like:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; IEnumerable&amp;lt;TResult&amp;gt; SelectWhere&amp;lt;TResult&amp;gt;(     
     &lt;span class="kwrd"&gt;this&lt;/span&gt; IEnumerable&amp;lt;TResult&amp;gt; source,     
     Func&amp;lt;TResult, &lt;span class="kwrd"&gt;bool&lt;/span&gt;&amp;gt; filter)
{

     var results = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;TResult&amp;gt;();

     &lt;span class="kwrd"&gt;foreach&lt;/span&gt;(var s &lt;span class="kwrd"&gt;in&lt;/span&gt; source)
          &lt;span class="kwrd"&gt;if&lt;/span&gt;(filter(s))
               results.Add(s);

     &lt;span class="kwrd"&gt;return&lt;/span&gt; results;

}&lt;/pre&gt;

&lt;p&gt;The above code defines an extension method named SelectWhere that accepts a generic argument called TResult.  TResult is used to flesh out the method's remaining arguments as well as its return type.&lt;/p&gt;

&lt;p&gt;The return type and first parameter (the one decorated with 'this') are obvious to us at this point.  The second parameter, Func&amp;lt;TResult, bool&amp;gt;, is simply a generic delegate type.  For a method to match the delegate's signature it must accept a single TResult parameter and return a bool.&lt;/p&gt;

&lt;p&gt;If we examine the implementation it is pretty straightforward.  We simply iterate over the entire collection and call the delegate for each item to determine if we need to save it.&lt;/p&gt;

&lt;p&gt;Now, how can we call this from client code?  There are a few ways, each of which is legal so let's start with the most explicit and work our way towards the sugar.&lt;/p&gt;

&lt;p&gt;First we need a method that matches the delegate to act as our filter criteria:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; PersonIsRich(Account acct) 
{

     &lt;span class="kwrd"&gt;return&lt;/span&gt; acct.Balance &amp;gt;= 10000D;

}&lt;/pre&gt;

&lt;p&gt;Your definition of a rich person may be different than mine, but I am sure you get the idea.  We return true if the account has 10,000 or more.&lt;/p&gt;

&lt;p&gt;Next we call the method on a collection of Accounts:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;var richPeople = SelectExtension.SelectWhere&amp;lt;Account&amp;gt;(allPeople, PersonIsRich);&lt;/pre&gt;

&lt;p&gt;As we can see the above is our more verbose usage and takes us back to the C function from before.  We need to pass in both the collection we are operating on as well as our filter.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;var richPeople = allPeople.SelectWhere&amp;lt;Account&amp;gt;(PersonIsRich);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;In this call we've used the extension method syntax and are just specifying the generic argument and the filter.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;var richPeople = allPeople.SelectWhere(PersonIsRich);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;At this point, we are beyond extension methods.  What just happened above is that the C# compiler is smart enough to figure out what our generic argument has to be in order to satisfy the extension method and doesn't bother making us put it there ourselves.  Logically speaking, if I am calling SelectWhere on an IEnumerable&amp;lt;Account&amp;gt;, then there is only one type that TResult can be, i.e. Account.  This is called 'type inference' and is the same reason that I can use the 'var' keyword instead of specifying the type of richPeople statically.  The compiler determines the type for me at compile time.&lt;/p&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;We have now seen what an extension method is and how it relates to LINQ.  We also now know that they are not magic and that we could get the same behavior from a normal, static method.  The new syntax helps add clarity though, and in development, clarity is a good thing.&lt;/p&gt;

&lt;p&gt;As a teaser for the next part, take a look at this:&lt;/p&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;pre class="csharpcode"&gt;var richPeople = allPeople.SelectWhere(acct =&amp;gt; acct.Balance &amp;gt;= 10000D);&lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;

&lt;p&gt;This is the final magic.  Where did our delegate function go? What is that crazy syntax?  Well, that is a lambda function and they will be the topic of my next post.&lt;/p&gt;

&lt;div class="wlWriterSmartContent" id="scid:18d43e01-4549-4fde-8ca6-c7b4b7385fac:a893f267-ffb3-45c1-9cbc-4f12420db3b4" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;
  &lt;p&gt;Download Solution - &lt;a href="http://geekswithblogs.net/images/geekswithblogs_net/dotnetnomad/WindowsLiveWriter/LINQOverviewpartoneExtensionMethods_10FE4/LinqOverview_3.zip"&gt;LinqOverview.zip&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }

]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; } 

]]&gt;&lt;/style&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=116793"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=116793" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/dotnetnomad/aggbug/116793.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>newman</dc:creator>
            <guid>http://geekswithblogs.net/dotnetnomad/archive/2007/11/12/116793.aspx</guid>
            <pubDate>Mon, 12 Nov 2007 15:08:27 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/dotnetnomad/comments/116793.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/dotnetnomad/archive/2007/11/12/116793.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/dotnetnomad/comments/commentRss/116793.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/dotnetnomad/services/trackbacks/116793.aspx</trackback:ping>
        </item>
        <item>
            <title>LINQ Overview, part zero</title>
            <link>http://geekswithblogs.net/dotnetnomad/archive/2007/11/09/116739.aspx</link>
            <description>&lt;h4&gt;&lt;/h4&gt;  &lt;h5&gt;Forward links&lt;/h5&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/dotnetnomad/archive/2007/11/12/116793.aspx"&gt;LINQ Overview, part one (Extension Methods)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://geekswithblogs.net/dotnetnomad/archive/2008/01/29/119037.aspx"&gt;LINQ Overview, part two (Lambda Expressions)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;I realize that these next few posts may be late to the game.  The LINQ CTPs for .NET 3.0 have been out for quite a while and everyone already knows about the massive amount of improvements Microsoft made in the Beta1 &amp;amp; Beta2 releases of .NET 3.5.  Further, we are supposedly less than a month away from seeing the official Visual Studio 2008 release.  All that being said I am going to spend a little time introducing the foundational pieces of LINQ so that I can lay the ground work for a more ambitious series.&lt;/p&gt;  &lt;p&gt;First, it is important to understand that LINQ is not magic.  It is purely syntactic sugar and is built from the ground up using some new primitives introduced in .NET 3.0/3.5.  The three most important are:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Extension Methods &lt;/li&gt;    &lt;li&gt;Lambda Expressions &lt;/li&gt;    &lt;li&gt;Anonymous Types &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;To tie it all together the LINQ team added several new keywords to the .NET language pool, but don't be mistaken, these are just conveniences like property getters and setters or the event keyword.  Everything done using the keywords can be done using raw code.&lt;/p&gt;  &lt;p&gt;My plan is to go from the ground up and demonstrate each new language feature briefly.  Doing so will allow us to examine compile time LINQ usage with all the cards on the table and nothing hidden behind syntax.&lt;/p&gt;  &lt;p&gt;While I take a day to put together part one, you might find the following links interesting.  Some of them are a bit heavy, but they are all valuable.  You'll also want to have access to some variant of Visual Studio 2008.  The Express versions are available free from Microsoft.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx" target="_blank"&gt;Visual Studio 2008 Beta2&lt;/a&gt; - The Express editions are always going to be free and the downloads are a lot smaller, I tend to stick with the Professional versions as I get them via MSDN.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx" target="_blank"&gt;Official LINQ Project website&lt;/a&gt; - Contains basic documentation and downloadable samples.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/mattwar/" target="_blank"&gt;Matt Warren's Blog&lt;/a&gt; - High upper on the LINQ Project team, invaluable.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=116739"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=116739" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/dotnetnomad/aggbug/116739.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>newman</dc:creator>
            <guid>http://geekswithblogs.net/dotnetnomad/archive/2007/11/09/116739.aspx</guid>
            <pubDate>Fri, 09 Nov 2007 15:31:59 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/dotnetnomad/comments/116739.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/dotnetnomad/archive/2007/11/09/116739.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/dotnetnomad/comments/commentRss/116739.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/dotnetnomad/services/trackbacks/116739.aspx</trackback:ping>
        </item>
    </channel>
</rss>