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;
            }
        }
      }
  • 相关阅读:
    Python基础实例001:数字组合问题
    Python集合
    标量、向量、矩阵、张量
    re模块函数之search
    Python常用字符串操作
    Python基础之元组
    Bai, IEEE 2019
    词嵌入
    RNN 训练时梯度爆炸和梯度消失的理解
    OCR 综述
  • 原文地址:https://www.cnblogs.com/aijiao/p/11982988.html
Copyright © 2011-2022 走看看