Lambda運算式,是一個匿名函數,它可以包含運算式和語句,並且可用於建立委託或運算式分類樹類型。
Lambda運算式, 都是用"=>"運算子。 讀作"goes to"。Lambda運算式運算子的左邊是輸入參數(如果有),右邊包含運算式或語句塊。使用格式如下:
(input parameters) => expression;
“Lambda運算式”是委託的實現方法,所以必須遵循以下規則:
大家都知道,一個類的私人成員只能在他的內部訪問!
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Lambda{ delegate bool D(); delegate bool D2(int i); class Test { public D del; public D2 del2; public void TestMethod(int input) { int j = 0; del = () => { j = 10; return j > input; }; del2 = (x) => { return x == j; }; Console.WriteLine("j = {0}", j); bool boolResult = del(); Console.WriteLine("j = {0}, b = {1}", j, boolResult); } } class Program { static void Main(string[] args) { Test test = new Test(); test.TestMethod(5); bool result = test.del2(10); Console.WriteLine(result); Console.ReadKey(); } }}私人成員,在類外面不能引用.private int x;
Cla cla = new Cla()
cla.x 這樣引用是錯的
如果public int x;
Cla cla = new Cla()
cla.x 這樣引用是對的
You can also create an anonymous method using an operator called lambda and represented by =>. From our example above, to use the lambda operator to create an anonymous method, omit the delegate keyword and follow the parentheses by the operator. Here is an example: