CSharp——Lambda 運算式

來源:互聯網
上載者:User
1、定義:

           a、Lambda 運算式是一個匿名運算式。

           b、可以包含運算式和語句

           c、可用於建立委託或運算式分類樹類型

           d、文法:Input params => Expression or Code Block. (左邊是資料參數,右邊是運算式或語句塊。)

                          Eg:

           //多個參數            (x, y) => x == y            //No params            (int x, string s) => s.Length > x            //無參數,註:只有一個參數時才能省略掉括弧:x => x*x             () => SomeMethod()             //語句塊。 註:語句塊中使用了大括弧。             (input parameters) => {statement;}
     註:Lambda 語句同匿名方法一樣無法用於建立運算式分類樹。
2、簡單用途

       a、委託中使用(Lambda運算式)

       delegate int del(int i);       del myDelegate = x => x * x;       int j = myDelegate(5); //j = 25

       b、委託未使用(Lambda運算式)

       delegate int del(int i);       del myDelegate = delegate(int i) {i*i };  //匿名方法       int j = myDelegate(5); //j = 25
    c、建立運算式分類樹類型:
         using System.Linq.Expressions;         // ...         Expression<del> = x => x * x;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.