zoukankan      html  css  js  c++  java
  • lambda表达式

    如其他地方所述,委托是一种包装方法调用的类型。就像类型一样,可以在方法之间传递委托实例,并且可以像方法一样调用委托实例。匿名函数是一个内联语句或表达式,可在需要委托类型的任何地方使用。可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数。

    共有两种匿名函数

    ·         Lambda 表达式(C# 编程指南).

    ·         匿名方法(C# 编程指南)

    C# 1.0 中,您通过使用在代码中其他位置定义的方法显式初始化委托来创建委托的实例。C# 2.0 引入了匿名方法的概念,作为一种编写可在委托调用中执行的未命名内联语句块的方式。C# 3.0 引入了 Lambda 表达式,这种表达式与匿名方法的概念类似,但更具表现力并且更简练。这两个功能统称为匿名函数。通常,针对 .NET Framework 版本 3.5 及更高版本的应用程序应使用 Lambda 表达式。

    下面的示例演示了从 C# 1.0 C# 3.0 委托创建过程的发展:

    class Test

    {

        delegate void TestDelegate(string s);//定义委托

        static void M(string s) //方法

    {

            Console.WriteLine(s);

        }

     

        static void Main(string[] args)

        {

            // Original delegate syntax required

            // initialization with a named method.

            TestDelegate testdelA = new TestDelegate(M);

     

            // C# 2.0: A delegate can be initialized with

            // inline code, called an "anonymous method." This

            // method takes a string as an input parameter.

            TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

     

            // C# 3.0. A delegate can be initialized with

            // a lambda expression. The lambda also takes a string

            // as an input parameter (x). The type of x is inferred by the compiler.

            TestDelegate testDelC = (x) => { Console.WriteLine(x); };

     

            // Invoke the delegates.

            testdelA("Hello. My name is M and I write lines.");

            testDelB("That's nothing. I'm anonymous and ");

            testDelC("I'm a famous author.");

     

            // Keep console window open in debug mode.

            Console.WriteLine("Press any key to exit.");

            Console.ReadKey();

        }

    }

    /* Output:

        Hello. My name is M and I write lines.

        That's nothing. I'm anonymous and

        I'm a famous author.

        Press any key to exit.

    */

    Lambda 表达式(C# 编程指南)

    “Lambda 表达式是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型。

    所有 Lambda 表达式都使用 Lambda 运算符 =>该运算符读为“goes to”。该 Lambda 运算符的左边是输入参数(如果有),右边包含表达式或语句块。Lambda 表达式 x => x * x 读作“x goes to x times x”。可以将此表达式分配给委托类型,如下所示:

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

    项目用例: 

    channle.Opening += ( sender, e ) => { LoggerPreparation.Write( "开始启动DCAU的Proxy" ); };

    分解

    static void channle_Opening(object sender, EventArgs e)

    {

       LoggerPreparation.Write("开始启动DCAU的Proxy");

    }

    channle.Opening += new EventHandler(channle_Opening);

    参考地址

    http://msdn.microsoft.com/zh-cn/library/bb397687.aspx

     

    天道酬勤

  • 相关阅读:
    Visual Studio 2019 开发 Python 及
    Delphi 的环境配置
    Delphi 的 TMS 控件安装方法
    网络爬虫-书籍与框架
    建筑行业(项目管理) BI 数据可视化分析案例
    DeepNude(一健脱衣)的算法以及通用图像生成的理论与实践研究
    matlab生成HEX文件-任意信号 大于64K长度
    SDRAM总结
    function [ binary,decimal ] = num2binary16( number )
    任意时钟分频
  • 原文地址:https://www.cnblogs.com/jeffrey725/p/1522264.html
Copyright © 2011-2022 走看看