Artigo Principal - As Novas Funcionalidades do C# 3.0 Article Home - The New Features of C # 3.0
Uma expressão Lambda é uma função anónima que pode conter expressões e declarações, e pode ser usada para criar delegates ou expression tree types . An expression Lambda is an anonymous function that may contain expressions and declarations, and can be used to create delegates or expression tree types.
As expressões Lambda usam o operador =>. The terms Lambda use the operator =>. O lado esquerdo do operador lambda especifica os parâmetros de entrada eo lado direito especifica a expressão. The left side of the operator lambda specifies the parameters of entry and the right side specifies the term. Por exemplo, a expressão Lambda x=>x*x . For example, the expression Lambda = x> x * x.
Passemos a um exemplo prático: Now a practical example:
using System; namespace ConsoleApplication { //Declare a 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(); } } } 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 ();))) Como podem ver pelo exemplo anterior associámos a expressão Lambda a um delegate type criado. As you can see the example above the words Lambda associámos a delegate type created.
Outra forma de utilizar as Lamba Expressions, é através generic delegates, tornando a declaração ainda mais concisa, neste caso até inline . Another way to use the Lamba Expressions, is through generic delegates, making the statement even more concise in this case until inline.
using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { //Generic delegate and lambda expression Func < int , int > AoQuadrado = x => x*x; Console.WriteLine(AoQuadrado(4)); Console.ReadKey(); } } } using System; namespace ConsoleApplication class Program ((static void Main (string [] args) (/ / Generic delegate and lambda expression Func <int, int> AoQuadrado = = x> x * x; Console.WriteLine (AoQuadrado (4)) ; Console.ReadKey ();))) Na minha opinião, um dos sitios em que as Lambda Expressions dão imenso jeito, é para efectuar filtros em determinadas querys, pois são bastante intuitivas e compactas. In my opinion, one of the sites where the Lambda Expressions give huge way, is to make filters in certain querys because it is very intuitive and compact.
Vejam o exemplo seguinte onde queremos descobrir quantos números são maiores que 32. Consider the following example where we want to find out how many numbers are greater than 32.
using System; using System.Linq; namespace ConsoleApplication { class Program { static void Main(string[] args) { // Data source. using System; using System.Linq; namespace ConsoleApplication 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 forces iteration of the source int highScoreCount = scores.Where(n => n > value).Count(); Console.WriteLine(”Existem {0} valores maiores que {1}”, highScoreCount, value); Console.ReadKey(); } } } int [] = (scores 93, 90, 82, 75, 71, 50, 33, 21); const int value = 32, / / The call to Count forces iteration of the source int highScoreCount = scores.Where (n => n> value). Count (); Console.WriteLine ( "There are (0) (values greater than 1)", highScoreCount, value); Console.ReadKey ();)))
Atenção que tem de utilizar o “System.Linq” para terem disponível a função Where . Note that you must use "System.Linq" to have the function Where available. É muito mais simples efectuar um filtro deste género, apenas numa linha de código. It is much simpler make a filter of this kind, only one line of code. Algo que era muito mais complexo antes das lambda expressions. Something that was much more complex before the lambda expressions.
Depois de nos habituarmos às Lambda Expressions , pouparemos bastante trabalho no desenvolvimento. After the habituarmos the Lambda Expressions, spare a lot of work in development.
E vocês já utilizam lambda expressions ? And you already use lambda expressions?
Se gostaram deste artigo subscrevam a feed RSS do istomesmo, para ficarem actualizados sobre os últimos artigos. If you liked this article subscribe to the RSS feed istomesmo, to be updated on the latest articles.
» Subscrevam aqui a fedd RSS « "Subscribe here fedd RSS '
Obtenha mais informações sobre a fedd RSS aqui Learn more about the RSS fedd here


Últimos Comentários Last comments