📜  C#中的Lambda表达式

📅  最后修改于: 2021-05-29 18:31:12             🧑  作者: Mango

C#中的Lambda表达式就像匿名函数一样使用,不同之处在于,在Lambda表达式中,您无需指定输入值的类型,从而使其更灵活地使用。
‘=>’是在所有lambda表达式中使用的lambda运算符。 Lambda表达式分为两部分,左侧是输入,右侧是表达式。

Lambda表达式可以有两种类型:

  1. 表达式Lambda:由输入和表达式组成。

    句法:

    input => expression;
    
  2. 语句Lambda:由输入和要执行的一组语句组成。

    句法:

    input => { statements };
    

让我们举一些例子来更好地理解上述概念。

示例1:在下面给出的代码中,我们有一个整数列表。第一个lambda表达式计算每个元素的平方{x => x * x},第二个lambda表达式用于查找可以被3整除的值{x =>(x%3)== 0}。并且foreach循环用于显示。

// C# program to illustrate the
// Lambda Expression
using System;
using System.Collections.Generic;
using System.Linq;
  
namespace Lambda_Exressions {
class Program {
    static void Main(string[] args)
    {
        // List to store numbers
        List numbers = new List() {36, 71, 12, 
                             15, 29, 18, 27, 17, 9, 34};
  
        // foreach loop to dislay the list
        Console.Write("The list : ");
        foreach(var value in numbers)
        {
            Console.Write("{0} ", value);
        }
        Console.WriteLine();
  
        // Using lambda expression
        // to calculate square of
        // each value in the list
        var square = numbers.Select(x => x * x);
  
        // foreach loop to display squares
        Console.Write("Squares : ");
        foreach(var value in square)
        {
            Console.Write("{0} ", value);
        }
        Console.WriteLine();
  
        // Using Lambda exression to
        // find all numbers in the list
        // divisible by 3
        List divBy3 = numbers.FindAll(x => (x % 3) == 0);
  
        // foreach loop to display divBy3
        Console.Write("Numbers Divisible by 3 : ");
        foreach(var value in divBy3)
        {
            Console.Write("{0} ", value);
        }
        Console.WriteLine();
    }
}
}
输出:
The list : 36 71 12 15 29 18 27 17 9 34 
Squares : 1296 5041 144 225 841 324 729 289 81 1156 
Numbers Divisible by 3 : 36 12 15 18 27 9 

示例2: Lambda表达式也可以与用户定义的类一起使用。下面给出的代码显示了如何基于定义列表的类的属性对列表进行排序。

// C# program to illustrate the
// Lambda Expression
using System;
using System.Collections.Generic;
using System.Linq;
  
// User defined class Student
class Student {
      
    // properties rollNo and name
    public int rollNo
    {
        get;
        set;
    }
      
    public string name
    {
        get;
        set;
    }
}
  
class GFG {
      
    // Main Method
    static void Main(string[] args)
    {
        // List with eah element of type Student
        List details = new List() {
            new Student{ rollNo = 1, name = "Liza" },
                new Student{ rollNo = 2, name = "Stewart" },
                new Student{ rollNo = 3, name = "Tina" },
                new Student{ rollNo = 4, name = "Stefani" },
                new Student { rollNo = 5, name = "Trish" }
        };
  
        // To sort the details list 
        // based on name of student
        // in acsending order
        var newDetails = details.OrderBy(x => x.name);
  
        foreach(var value in newDetails)
        {
            Console.WriteLine(value.rollNo + " " + value.name);
        }
    }
}
输出:
1 Liza
4 Stefani
2 Stewart
3 Tina
5 Trish