Lambda expression for multiple parameters

后端 未结 3 926
一向
一向 2021-02-03 21:27

I understand a lambda expression is in essence an inline delegate declaration to prevent the extra step

example

delegate int Square(int x)
public class          


        
相关标签:
3条回答
  • 2021-02-03 22:09

    You must understand the Func behavior, where the last parameter is always the output or result

    Func<1, 2, outPut>

    Func<int, int, int> Add = (x, y) => x + y;
    
    Func<int, int, int> diff = (x, y) => x - y;
    
    Func<int, int, int> multi = (x, y) => x * y;
    
    0 讨论(0)
  • 2021-02-03 22:14

    Yes. When you have other-than-one (zero, or > 1) lambda arguments, use parenthesis around them.

    Examples

    Func<int, int, int> add = (a,b) => a + b;
    
    int result = add(1, 3);
    
    Func<int> constant = () => 42;
    
    var life = constant();
    
    0 讨论(0)
  • 2021-02-03 22:17
    delegate int Multiplication(int x, int y)
    public class Program
    {
       static void Main(String[] args)
       {
          Multiplication s = (o,p)=>o*p;
          int result = s(5,2);
          Console.WriteLine(result); // gives 10
       }
    }
    
    0 讨论(0)
提交回复
热议问题