zoukankan      html  css  js  c++  java
  • 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);
        }
      }
    }

    反编译结果是帮我们定义了几个变量。

    作者:芝麻科技
    出处:芝麻麻雀-Asp.Net学习之路
    技术:C++,C#
    向我打赏
    加我微信,聊一聊技术
  • 相关阅读:
    Burnside引理与Polya定理 学习笔记
    Codeforces 438E. The Child and Binary Tree 多项式,FFT
    Berlekamp_Massey 算法 (BM算法) 学习笔记
    UOJ#335. 【清华集训2017】生成树计数 多项式,FFT,下降幂,分治
    UOJ#73. 【WC2015】未来程序 提交答案题
    UOJ#206. 【APIO2016】Gap 构造 交互题
    虚拟机配置JAVA_HOME
    创建虚拟机
    月份、季度、周
    maven多模块下使用JUnit进行单元测试
  • 原文地址:https://www.cnblogs.com/mzy-google/p/9323056.html
Copyright © 2011-2022 走看看