In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the concept of C # Lambda Expression". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the concept of C # Lambda Expression"?
1. About C # Lambda Expressions:
A Lambda _ Expression (translated as Lambda) is an anonymous function that contains several expressions and statements. Can be used to create a delegate object or expression tree type. All Lambda forms use the operator "= >", which means "goes to". The left part of the operator is the input parameter table, and the right part is the expression or statement block. X = > x * x read "x transformed into x multiplied by x". The Lambda form can be assigned to a delegate type:
C# Lambda instance 1:
Delegate int del (int I); del myDelegate = x = > x * x; int j = myDelegate (5); / / j = 25
It can also be used to create an expression tree type:
C# Lambda instance 2:
Using System.Linq.Expressions; / /... Expression = x = > x * x
The operator "= >" has the same operation priority as "=" and is related to the right (execute first on the right).
In example 1, we notice that the definition of the delegate has an input parameter of type int and a return value of type int. There is no declaration of any type in the Lambda form in the example. The compiler does the corresponding implicit data type conversion for us: the input parameter type can be implicitly converted from the input parameter type of the delegate, and the return type can be implicitly converted to the return type of the delegate.
The Lambda type is not allowed as the left Operand of the "is" and "as" operators. That is,
Del myDelegate = x = > x * x as string; / / error
All constraints on anonymous methods also apply to Lambda. See Anonymous Methods (C # Programming Guide).
two。 Understanding C # Lambda Expressions from expressions
An Lambda form consisting of an evaluated expression is called the expression Lambda. The expression Lambda is often used to construct expression trees. An expression Lambda returns the result of evaluating the expression. The basic structure is as follows:
(input parameters) = > expression / / if there is only one input parameter, parentheses can be omitted. / / if you have more than one input parameter, you must add parentheses. (X) = > x * x equals x = > x * x (x, y) = > x = = y / / the type of input parameters can be explicitly specified (int x, string s) = > s.Length > x / / or without any input parameters () = > SomeMethod1 ()
The above code calls a method in Lambda. It is important to note that if you are creating an expression tree that will be used by other parties, it is not appropriate to perform method calls in the Lambda style. For example: execute in SQL Server.
In general, there is no point in letting a method execute outside the context of the original design, nor does it really work.
3. Understanding C # Lambda Expressions from sentences
The statement Lambda is very similar to the expression Lambda except that the statement is enclosed in curly braces:
(input parameters) = > {statement;}
Statements in curly braces can be as many as you want, or they can be written as multiple lines (defining a Lambda style, that is, defining an anonymous method):
TestDelegate myDel = n = > {string s = n + "+" World "; Console.WriteLine (s);}
Of course, the statement Lambda, like anonymous methods, cannot be used to create an expression tree.
Type conjecture of 4.C# Lambda Expressions
When writing a Lambda, we usually do not need to explicitly specify the type of input parameters. Because the compiler guesses the type based on the implementation of the Lambda body and the definition of the delegate.
For example: if you want to delete elements less than 100 from a List
Lst.RemoveAll (I = > I)
< 100); //i会被猜测为int 通常的猜测规则如下: ◆Lambda式必须包含与委托定义中相等数量的输入参数; ◆每个Lambda式的输入参数必须能够隐式转换成委托定义中所要求的输入参数; ◆Lambda式的返回值必须能够隐式转换成委托定义中的返回值。 注意:由于目前在common type system中还没有一个"Lambda式类型"的类型。如果在有些场合提到"Lambda式的类型",那通常表示委托的定义或者是Expression类型。 5.C# Lambda Expressions变量作用域 在Lambda式定义中可以引用外部变量。只要是在定义处能够访问到的变量,都可以在Lambda式中引用。 Lambda式的定义仅仅是定义一个匿名方法,最终会生成一个委托对象。外部变量的引用将被"捕获"到委托对象内部,将会伴随委托对象的整个生命周期。在委托对象生命周期结束之前该变量都不会被垃圾回收。就算外部变量已经超过了原来的作用域,也还能继续在Lambda式中使用。所有会被引用的外部变量必须在Lambda式定义之前被显式赋值。见下例 delegate bool D(); delegate bool D2(int i); class Test { D del; D2 del2; public void TestMethod(int input) { int j = 0; // Initialize the delegates with lambda expressions. // Note access to 2 outer variables. // del will be invoked within this method. del = () =>{j = 10; return j > input;}; / / del2 will be invoked after TestMethod goes out of scope. Del2 = (x) = > {return x = = j;}; / / Demonstrate value of j: / / Output: J = 0 / / The delegate has not been invoked yet. Console.WriteLine ("j = {0}", j); / / Invoke the delegate. Bool boolResult = del (); / / Output: J = 10 b = True / / Note j is modified during the execution of del Console.WriteLine ("j = {0}. B = {1}", j, boolResult);} static void Main () {Test test = new Test (); test.TestMethod (5); / / Prove that del2 still has a copy of / / local variable j from TestMethod. The reference to / / j exceeds the previously defined scope bool result = test.del2 (10); / / Output: True Console.WriteLine (result); Console.ReadKey ();}}
Here are the rules about the scope of variables:
Variables that are "captured" in ◆ will not be garbage collected until the end of the delegate's life cycle.
Variables defined by ◆ within the Lambda style are not visible to the outside
◆ Lambda cannot directly capture a parameter variable with a ref or out description
The return statement in ◆ Lambda does not cause the current method to return
The inclusion of goto,break or continue statements that cause the current execution range to be skipped is not allowed in ◆ Lambda.
At this point, I believe you have a deeper understanding of "what is the concept of C # Lambda Expression". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.