1. 程式人生 > >System.Action的使用(lambda 表示式)

System.Action的使用(lambda 表示式)

對於Action的使用方法使用如下:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string first = "First";
            var action = new Action(() => { Console.WriteLine(first); });
            action();

            var action2 = new Action<string
>((s) => { Console.WriteLine($"Action<T>:{s}"); }); action2(first); var action3 = new Action<string, string>((s1, s2) => { Console.WriteLine($"Action<T1,T2>:{s1},{s2}"); }); action3(first, "second"); } } }

使用dotPeek通過反編譯,得到程式碼:

namespace ConsoleApp1
{
  internal class Program
  {
    private static void Main(string[] args)
    {
      string first = "First";
      ((Action) (() => Console.WriteLine(first)))();
      ((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}
", (object) s))))(first); ((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(first, "second"); } } }

下面寫一種與反編譯出來的相似的方法

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string first = "First";
            var action = new Action(() => { Console.WriteLine(first); });
            action();

            var action2 = new Action<string>((s) => { Console.WriteLine($"Action<T>:{s}"); });
            action2(first);

            var action3 = new Action<string, string>((s1, s2) =>
            {
                Console.WriteLine($"Action<T1,T2>:{s1},{s2}");
            });
            action3(first, "second");

            new Action(() => { Console.WriteLine(first); })();
            new Action<string>((s) => { Console.WriteLine($"Action<T>:{s}"); })(first);
            new Action<string, string>((s1, s2) =>
            {
                Console.WriteLine($"Action<T1,T2>:{s1},{s2}");
            })(first, "second");
        }
    }
}

看一下反編譯的結果:

namespace ConsoleApp1
{
  internal class Program
  {
    private static void Main(string[] args)
    {
      string first = "First";
      ((Action) (() => Console.WriteLine(first)))();
      ((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}", (object) s))))(first);
      ((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(first, "second");
      ((Action) (() => Console.WriteLine(first)))();
      string str1 = first;
      ((Action<string>) (s => Console.WriteLine(string.Format("Action<T>:{0}", (object) s))))(str1);
      string str2 = first;
      string str3 = "second";
      ((Action<string, string>) ((s1, s2) => Console.WriteLine(string.Format("Action<T1,T2>:{0},{1}", (object) s1, (object) s2))))(str2, str3);
    }
  }
}

反編譯結果是幫我們定義了幾個變數。

相關推薦

System.Action的使用lambda 表示式

對於Action的使用方法使用如下: namespace ConsoleApp1 { class Program { static void Main(string[] args) { string first = "First";

java8學習筆記1Lambda表示式

Lambda 表示式 Lambda 表示式,也可稱為閉包,它是推動 Java 8 釋出的最重要新特性。Lambda 允許把函式作為一個方法的引數(函式作為引數傳遞進方法中)。使用 Lambda 表示式可以使程式碼變的更加簡潔緊湊。 語法 lambda 表示式的語法格式如

jdk8新特性Lambda表示式結合spring 執行緒池,一行程式碼實現多執行緒

1.配置spring 執行緒池 @Configuration @EnableAsync @ConfigurationProperties(prefix="threadpool") public class ExecutePoolConfiguration { @V

JavaScript 箭頭函式Lambda表示式

簡介 JavaScript 中,函式可以用箭頭語法(”=>”)定義,有時候也叫“lambda表示式”。這種語法主要意圖是定義輕量級的內聯回撥函式。例如: // Arrow function: [5, 8, 9].map(item => ite

Java™ 教程Lambda表示式

Lambda表示式 匿名類的一個問題是,如果匿名類的實現非常簡單,例如只包含一個方法的介面,那麼匿名類的語法可能看起來不實用且不清楚,在這些情況下,你通常會嘗試將方法作為引數傳遞給另一個方法,例如當有人單擊按鈕時應採取的操作,Lambda表示式使你可以執行此操作,將

最全最強 Java 8 - 函式程式設計lambda表示式

Java 8 - 函式程式設計(lambda表示式) 我們關心的是如何寫出好程式碼,而不是符合函式程式設計風格的程式碼。 @pdai Java 8 - 函式程式設計(lambda表示式) 簡介 lambda表示式 分類 惰性求值方法 及早求值方法 stream & parallelStre

java學習筆記-- java新特性 列舉 & 註解 & 介面定義加強 & Lambda表示式

列舉 (enum) 高階的多例模式 java中列舉使用enum關鍵字定義列舉 列舉就是一種多例設計模式 enmu Color{     RED,BLUE,GREEN;     } enum Color{

Java 8 新特性:Lambda 表示式之方法引用Lambda 表示式補充版

方法引用 文 | 莫若吻      (注:此文乃個人查詢資料然後學習總結的,若有不對的地方,請大家指出,非常感謝!) 1.方法引用簡述 方法引用是用來直接訪問類或者例項的已經存在的方法或

從委託到 lambda 表示式介紹為什麼需要 lambda 表示式

接下來,筆者將以簡單的說話方式向你介紹java8的新特性,lambda表示式。 用最簡單的方式告訴你為什麼需要 lambda 表示式 簡單地來解釋委託,就是“把方法作為引數傳遞”。 歷史來源: 有時候需要傳入一段程式碼到程式的方法中。

Python 藉助逆波蘭表示式字尾表示式實現簡單計算器

Python 藉助逆波蘭表示式(字尾表示式)實現簡單計算器 文章目錄 Python 藉助逆波蘭表示式(字尾表示式)實現簡單計算器 0. 參考資料 1. 中綴表示式轉字尾表示式 2. 字尾表示式的求值 3. Python

coursera公開課——recommender system作業第二週

寫這麼醜的程式碼我也是醉了,繼續學習。 第二週的assignment: Mean Rating: Calculate the mean rating for each movie, or

逆波蘭式字尾表示式的表達求值

逆波蘭表示式求值 [編輯]虛擬碼 while有輸入符號 讀入下一個符號IF是一個運算元 入棧ELSE IF是一個操作符 有一個先驗的表格給出該操作符需要n個引數IF堆疊中少於n個運算元 (錯誤) 使用者沒有輸入足夠的運算元Else,n個操作數出棧計算操作符。將計算所得的

Python匿名函式lambda函式

匿名函式lambda Python使用lambda關鍵字創造匿名函式。所謂匿名,意即不再使用def語句這樣標準的形式定義一個函式。這種語句的目的是由於效能的原因,在呼叫時繞過函式的棧分配。其語法是: lambda [arg1[, arg2, ... ar

odoo10學習筆記1Domain表示式

今天學習下Domain表示式。 什麼是Domain [('create_uid','=',user.id)] Domain是個多條件的列表,每個條件是一個三元表示式:[(欄位名,操作符,值), (欄位名,操作符,值)] Domain使用場合 許可權管理中

C# Action 委託 + lambda 表示式

using System; using System.Collections; public class Test { static public Action A; static public Action<int> B; stati

Netty in Action 十六 第六章節 第二部分 ChannelHandlerContext和異常處理

6.3 Interface ChannelHandlerContext 一個ChannelHandlerContext代表了一個ChannelHandler和ChannelPipeline之間的關係,ChannelHandlerContext創建於ChannelHandl

Python 比較兩個數的大小三元表示式

利用三元表示式來比較Python中兩個數的大小: 尤其注意最後面沒有冒號。 def f(a,b): # 在此處需要加return來作返回值,也可以將結果賦值給一變數 # 如: x = (a,b) if a>b else (b,a) return (a,b) if

Struts2的使用註解配置Action零配置

1,首先引入struts2註解的jar包:struts2-convention-plugin.jar <!-- struts2註解 --> <dependency> <groupId>org.apache.str

歐幾里得演算法除法表示式

題意: 給出一個這樣的除法表示式:X1/X2/X3/···/Xk,其中Xi是正整數。除法表示式應當按照從左到右的順序求和,例如表示式1/2/1/2的值為1/4。但是可以在表示式中嵌入括號以改變計算順序

Netty in Action 十四 第五章節 第三部分 ByteBufHolder,ByteBuf分配,計數引用

5.4 Interface ByteBufHolder 我們經常在ByteBuf中儲存一些正常資料之外,我們有時候還需要增加一些各式各樣的屬性值,一個Http響應體就是一個很好的例子,除了按照位元組傳輸過來的主體內容,還有狀態碼,cookie等資訊 Netty提供了By