C# Lambda表达式

一、Lambda表达式,1.匿名方法,2.Inline方法

第一种:

            Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a + b; });
            int res = func(100, 200);
            Console.WriteLine(res);
            func = new Func<int, int, int>((int x, int y) => { return x * y; });
            res = func(3, 4);
            Console.WriteLine(res);

这个结果是:

 同等的第一种:

            Func<int, int, int> func =(a, b) => { return a + b; };
            int res = func(100, 200);
            Console.WriteLine(res);
            func = (x, y) => { return x * y; });
            res = func(3, 4);
            Console.WriteLine(res);

结果是一样的:

第二种:

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

namespace kongzhitaiApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Lambda表达式,
            //1.匿名方法,
            //2.Inline方法        
            DoSomeCalc<int>((int a, int b) => { return a * b; },10, 200);
            //DoSomeCalc<int>((a, b) => { return a * b; }, 10, 200);//int可以省略不写
            #region  //有名字不inline
            //int res = Add(100, 200);
            #endregion
        }

        static void DoSomeCalc<T>(Func<T,T,T>func,T x,T y)
        {
         T res=   func(x, y);
            Console.WriteLine(res);
        }
        //有名字不inline
        static int Add(int a,int b)
        {
            return a + b;
        }
    }
  }
原文地址:https://www.cnblogs.com/aijiao/p/11982988.html