Lambda实例

xiaoxiao2021-02-28  97

在 2.0 之前的 C# 版本中,声明委托的唯一方法是使用命名方法。  C# 2.0 引入了匿名方法,而在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方式  有一种情况下,匿名方法提供了 Lambda 表达式中所没有的功能。  您可使用匿名方法来忽略参数列表。  这意味着匿名方法可转换为具有各种签名的委托。  这对于 Lambda 表达式来说是不可能的。  有关 lambda 表达式的更多特定信息,请参见 Lambda 表达式(C# 编程指南)。 

    总结下红色那段话的意思:微软告诉你:我们在C#2.0之前就有委托了,在2.0之后又引入了匿名方法,C#3.0之后,又引入了Lambda表达式,他们三者之间的顺序是:委托->匿名变量->Lambda表达式,微软的一步步升级,带给我们编程上的优美,简洁,可读性强.....

    温故而知新,咱们来温故下委托和匿名表达式。

委托如下:

delegate int calculator(int x, int y); //委托类型 static void Main() { calculator cal = new calculator(Adding); int He = cal(1, 1); Console.Write(He); } /// <summary> /// 加法 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public static int Adding(int x, int y) { return x + y; }

匿名方法如下:

delegate int calculator(int x, int y); //委托 static void Main() { calculator cal = delegate(int num1,int num2) { return num1 + num2; }; int he = cal(1, 1); Console.Write(he); }

下面我们来讲解Lambda表达式:

 按照上边的加法,我们用Lambda表达式来实现,代码如下:

delegate int calculator(int x, int y); //委托类型 static void Main() { calculator cal = (x, y) => x + y;//Lambda表达式,大家发现没有,代码一个比一个简洁 int he = cal(1, 1); Console.Write(he); }

那么我们详细讲讲Lambda表达式:

若要创建 Lambda 表达式,需要在 Lambda 运算符 => 左侧指定输入参数(如果有),然后在另一侧输入表达式或语句块。 例如,lambda 表达式 x => x * x 指定名为 x 的参数并返回 x 的平方值。 如上面的示例所示,你可以将此表达式分配给委托类型

"Lambda表达式"是一个特殊的匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量。它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。Lambda表达式x => x * x读作"x goes to x times x"。举几个简单的Lambda表达式,如下:

delegate bool MyBol(int x, int y); delegate bool MyBol_2(int x, string y); delegate int calculator(int x, int y); //委托类型 delegate void VS(); static void Main(string[] args) { MyBol Bol = (x, y) => x == y; Console.WriteLine(Bol(5,5)); MyBol_2 Bol_2 = (x, s) => s.Length > x; Console.WriteLine(Bol_2(2, "kone")); calculator C = (X, Y) => X * Y; calculator C2 = (x, y) => x * x * y; Console.WriteLine("C的值:" + C(5, 5)); Console.WriteLine("C2的值:" + C2(5, 5)); VS S = () => Console.WriteLine("我是无参数Labada表达式"); S(); Console.WriteLine("==================================================="); // int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddNumbers = numbers.Count(n => n % 2 == 1); // List<People> people = LoadData();//初始化 //匿名方法 //IEnumerable<People> results = people.Where(delegate(People p) { return p.age > 20; }); //Lambda表达式 IEnumerable<People> results = people.Where(w => w.age > 20); foreach (var result in results) { Console.WriteLine(result.name); } Console.ReadKey(); } private static List<People> LoadData() { List<People> people = new List<People>(); //创建泛型对象 People p1 = new People(21, "guojing"); //创建一个对象 People p2 = new People(21, "wujunmin"); //创建一个对象 People p3 = new People(20, "muqing"); //创建一个对象 People p4 = new People(23, "lupan"); //创建一个对象 people.Add(p1); //添加一个对象 people.Add(p2); //添加一个对象 people.Add(p3); //添加一个对象 people.Add(p4); return people; } public class People { public int age { get; set; } //设置属性 public string name { get; set; } //设置属性 public People(int age, string name) //设置属性(构造函数构造) { this.age = age; //初始化属性值age this.name = name; //初始化属性值name } }

Func<T>委托

 T 是参数类型,这是一个泛型类型的委托,用起来很方便的。

 先上例子

static void Main(string[] args) { Func<int, string> gw1 = delegate(int i) { return i.ToString(); }; Console.WriteLine(gw1(555)); //Func<int, string> gwl = p => p + 10 + "--返回类型为string"; Console.ReadKey(); }

说明:我们可以看到,这里的p为int 类型参数, 然而lambda主体返回的是string类型的。

再上一个例子

static void Main(string[] args) { Func<int, int, bool> gwl = (p, j) => { if (p + j == 10) { return true; } return false; }; Console.WriteLine(gwl(5,5) + ""); //打印‘True’,z对应参数b,p对应参数a Console.ReadKey(); }

转载请注明原文地址: https://www.6miu.com/read-73683.html

最新回复(0)