【讀書筆記】Lambda運算式

來源:互聯網
上載者:User

    Lambda運算式,是一個匿名函數,它可以包含運算式和語句,並且可用於建立委託或運算式分類樹類型。

    Lambda運算式, 都是用"=>"運算子。 讀作"goes to"。Lambda運算式運算子的左邊是輸入參數(如果有),右邊包含運算式或語句塊。使用格式如下:

(input parameters) => expression;
“Lambda運算式”是委託的實現方法,所以必須遵循以下規則:
  • 1)“Lambda運算式”的參數數量必須和“委託”的參數數量相同;
  • 2)如果“委託”的參數中包括有ref或out修飾符,則“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:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.