委派与匿名方法演化至Lambda运算式

摘要:委派与匿名方法演化至Lambda运算式


一、委派

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program pg = new Program();
            //建立委派实例对象
            helloDelegate myHelloDelegate = new helloDelegate(pg.ReturnMessage);//透过delegate类,保存特定方法的参照,这些参照对应至实例类所定义的方法成员

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadLine();
        }

        public string ReturnMessage(string pName)
        {
            return pName;
        }

        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数
    }
}

也可写成下列方式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数

        static void Main(string[] args)
        {
            //建立委派实例对象
            helloDelegate myHelloDelegate = ReturnMessage;//透过delegate类,保存特定方法的参照,这些参照对应至实例类所定义的方法成员

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadKey();
        }

        public string ReturnMessage(string pName)
        {
            return pName;
        }
    }
}

注意红色字会出错,原因为

“Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.ReturnMessage(string)' ”

所以ReturnMessage前必须要再加上static以声明为静态方法

public static string ReturnMessage(string pName)
{
    return pName;
}

二、匿名委派

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立委派实例对象
            helloDelegate myHelloDelegate = delegate(string pName)//匿名委派
            {
                return pName;
            };

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadLine();
        }

        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数
    }
}

参、Lambda运算式

1.Lambda运算式是由称之为“goes to”Lambda操作符“=>”所组成,

用以取代匿名方法所引用的delegate,

语法格式为(input parameters) => (expression)

变化如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立委派实例对象
            helloDelegate myHelloDelegate = (string pName) =>//Lambda运算式
            {
                return pName;
            };

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadLine();
        }

        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数
    }
}

2.Lambda运算式更可以缩成一行

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
//建立委派实例对象
            helloDelegate myHelloDelegate = (string pName) => { return pName; };//匿名委派

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadLine();
        }

        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数
    }
}

3.也可不需要大括号与return

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
//建立委派实例对象
            helloDelegate myHelloDelegate = (string pName) => pName; //匿名委派

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadLine();
        }

        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数
    }
}

4.可不指定输入类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//委派是事件的基础
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
//建立委派实例对象
            helloDelegate myHelloDelegate = pName => pName; //Lambda运算式

            //应用程序经由委派类的方法参照、引用方法,完成调用进程
            string message = myHelloDelegate("a");
            Console.WriteLine(message);
            Console.ReadLine();
        }

        //委派是一种链机制
        public delegate string helloDelegate(string pName);//声明指定参照方法的输入参数
    }
}

四、另一演化范例

例:取得每个人的名称

List 所提供的ConvertAll方法

var names =people.ConvertAll(p=>p.name);

Enumerable的select扩充方法

var names=Enumerable.select(people,p=>p.name);

使用比较直觉的写法

var names = people.Select(p=>p.name);

使用查询运算式

var names = from p in people select p.name;

参考数据:

Func 委派的用法

C# 笔记:Expression Trees

原文:大专栏  委派与匿名方法演化至Lambda运算式


猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11491069.html