Main Article - The New Features of C # 3.0
A Lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
Lambda expressions using the operator =>. The left side of the lambda operator specifies the input parameters and specifies the right expression. For example, the lambda expression x => x * x.
Now a practical example:
using System; namespace ConsoleApplication ( / / Declare the Delegate delegate int Del (int x); class Program ( static void Main (string [] args) ( / / Create the Lambda Expression Del AoQuadrado = x => x * x; Console.WriteLine (AoQuadrado (4)); Console.ReadKey (); ) ) )
As you can see by the example previously associated with Lambda expression to a delegate type created.
Another way to use the Lamba Expressions, is through generic delegates, making the statement more concise in this case to inline.
using System; namespace ConsoleApplication ( class Program ( static void Main (string [] args) ( / / Generic delegate and lambda expression , int > AoQuadrado = x => x*x; Func <int, int> AoQuadrado = x => x * x; Console.WriteLine (AoQuadrado (4)); Console.ReadKey (); ) ) )
In my opinion, one of the sites where the Lambda Expressions provide huge way, is to make filters in certain querys therefore are very intuitive and compact.
See the following example where we want to find out how many numbers are greater than 32.
using System; using System.Linq; ConsoleApplication namespace (class Program (static void Main (string [] args) (/ / Data source. int [] scores = (93, 90, 82, 75, 71, 50, 33, 21 ); const int value = 32, / / The call to Count Iteration forces of the source highScoreCount int = scores.Where (n => n> value). Count () Console.WriteLine ( "(0) There are higher values that (1) ", highScoreCount, value); Console.ReadKey ();))) Note that you must use the "System.Linq" to have available the function where. It is very simple make such a filter, only one line of code. Something that was much more complex before lambda expressions.
Once you get used to Lambda Expressions, save a lot of work in development.
And you already use lambda expressions?
If you liked this article subscribe to the RSS feed istomesmo to stay updated on the latest articles.
Learn more about RSS fedde here
Articles that may interest you:



