Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, to start off with the scary definition - a lambda is another way of defining an anonymous method. There has (since C# 2.0 I believe) been a way to construct anonymous methods - however that syntax was very... inconvinient.</p> <p>So what is an anonymous method? It is a way of defining a method inline, with no name - hence being anonymous. This is useful if you have a method that takes a delegate, as you can pass this lambda expression / anonymous method as a parameter, given that the types match up. Take IEnumerable.Select as an example, it is defined as follows:</p> <pre><code>IEnumerable&lt;TResult&gt; Select&lt;TSource, TResult&gt;(this IEnumerable&lt;TSource&gt; source, Func&lt;TSource, TResult&gt; selector); </code></pre> <p>If you were to use this method normally on say a List and select each element twice (that is concatenated to itself):</p> <pre><code>string MyConcat(string str){ return str + str; } ... void myMethod(){ IEnumerable&lt;string&gt; result = someIEnumerable.Select(MyConcat); } </code></pre> <p>This is a very inconvinient way of doing this, especially if you wish to perform many of these operations - also typically these kinds of methods you only use once. The definition you showed (parameters => expression) is very consise and hits it spot on. The interesting thing about lambda is that you do not need to express the type of the parameters - <strong>as long as they can be infered from the expression</strong>. Ie. in Select's case - we <em>know</em> the first parameter must be of type TSource - because the definition of the method states so. Further more - if we call the method just as foo.Select(...) then the return value of the expression will define TResult. </p> <p>An interesting thing as well is that for one-statement lambda's the return keyword is not needed - the lambda will return whatever that one expression evaluates to. However if you use a block (wrapped in '{' and '}') then you must include the return keyword like usual.</p> <p>If one wishes to, it is still 100% legal to define the types of the parameters. With this new knowledge, let's try and rewrite the previous example:</p> <pre><code>void myMethod(){ IEnumerable&lt;string&gt; result = someIEnumerable.Select(s =&gt; s + s); } </code></pre> <p>Or, with explicit parameters stated</p> <pre><code>void myMethod(){ IEnumerable&lt;string&gt; result = someIEnumerable.Select((string s) =&gt; s + s); } </code></pre> <p>Another interesting feature of lambda's in C# is their use to construct <a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" rel="nofollow noreferrer">expression trees</a>. That is probably not "beginner" material though - but in short an expression tree contains all the meta data about a lambda, rather than executable code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload