zoukankan      html  css  js  c++  java
  • C# 委托的发展史: .Net 1.x 委托 =>.Net 2.x 匿名方法 => .Net 3.0/3.5 Lambda 表达式

    
    /*
            C# 委托的发展史: .Net 1.x 委托 =>.Net 2.x 匿名方法 => .Net 3.0/3.5 Lambda 表达式
        ms-help://MS.MSDNQTR.v90.chs/dv_csref/html/6ce3f04d-0c71-4728-9127-634c7e9a8365.htm
        在 C# 1.0 中,您通过使用在代码中其他位置定义的方法显式初始化委托来创建委托的实例。
        C# 2.0 引入了匿名方法的概念,作为一种编写可在委托调用中执行的未命名内联语句块的方式。
        C# 3.0 引入了 Lambda 表达式,这种表达式与匿名方法的概念类似,但更具表现力并且更简练。
        这两个功能统称为“匿名函数”。通常,针对 .NET Framework 版本 3.5 及更高版本的应用程序应使用 Lambda 表达式。
        下面的示例演示了从 C# 1.0 到 C# 3.0 委托创建过程的发展:
    */
    namespace Microshaoft
    {
        using System;
        class Test
        {
            //c# 1.0
            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.
        */
    }
    
    
  • 相关阅读:
    you must restart adb and eclipse的相关解决办法
    Qt slot中获取sender
    添加开机启动项
    Unreal开发HTC Vive程序,开启VR编辑模式
    Android弹出一项权限请求
    Unreal新建C++类或C++项目失败
    win10 设置C盘访问权限
    windows系统共享设置最顺的一次
    下载Qt安装包
    单例模式
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/1603585.html
Copyright © 2011-2022 走看看