zoukankan      html  css  js  c++  java
  • 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;
            }
        }
      }
  • 相关阅读:
    NPOI操作Excel
    父窗口调用iframe子窗口方法
    js 全选全不选
    常用的几种 SQLServer 分页查询方式实现
    通用简单的 分页 SQL
    C#导出
    delphi xe firemonkey 调用VLC播放器播放视频
    Android版本和API Level对应关系
    Android开发之视频录制1
    Android上实现视频录制
  • 原文地址:https://www.cnblogs.com/aijiao/p/11982988.html
Copyright © 2011-2022 走看看