首页 > 编程语言 >C# lambda+委托

C# lambda+委托

时间:2023-01-23 15:23:02浏览次数:37  
标签:Console 委托 C# System 123 WriteLine 返回值 using lambda

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

namespace learn_lambda2
{
    internal class Program
    {
        static void Main(string[] args)
        {

            // 单行, 无参数, 无返回值
            Action action = () => { Console.WriteLine("hello world"); };
            action();

            // 多行, 两个参数, 无返回值
            Action<int, string> action1 = new Action<int, string>((a, b)=> {
                a += a;
                Console.WriteLine($"{a} + {b}"); 
            });
            action1(123, "123");

            // 单行, 无参数, 有返回值
            Func<int> action2 = () => { return 123; };
            Console.WriteLine(action2());

            // 多行, 两个参数, 有返回值
            Func<int, string, int> action3 = new Func<int, string, int>((a, b) =>
            {
                a += a;
                return Convert.ToInt32(b) + a;
            });
            Console.WriteLine(action3(123, "123"));

            Console.ReadKey();
        }
    }
}

执行结果

hello world
246 + 123
123
369

标签:Console,委托,C#,System,123,WriteLine,返回值,using,lambda
From: https://www.cnblogs.com/yingyingdeyueer/p/17065211.html

相关文章

  • gtid的新特性assign_gtids_to_anonymous_transactions
    在MySQL8.0.23之前,想创建一个主从环境,主库不开启GTID、从库开启GTID,这是不可能的MySQL8.0.23中引入了一个新特性:assign_gtids_to_anonymous_transactions,支持主从复制环境......
  • C++堆与栈【cherno课程学习】
    C++的堆与栈当我们程序开始的时,它被分成了一堆不同的内存区域,除了堆和栈还有很多东西,但我们最关心的两个就是堆和栈在应用程序启动后,操作系统要做的就是,他会将整个郑......
  • 无法加载文件 C:\Users\Administrator\Desktop\spider01\venv\Scripts\activat
    遇到问题原因Restricted(防止运行没有数字签名的脚本),要设置成remotesigned模式解决方案输入get-executionpolicy以管理员的方式打开Powershall运行,并在命令窗......
  • 在尝试加载程序集 ID 65536 时 Microsoft .NET Framework 出错。服务器可能资源不足,或
    SqlServer 函数中执行的程序集但用户的权限不够,后DBA使用sa 账号设置了就对了网上找到的解决方法:这数据库是从其他数据库还原到本地数据库的,不少网友说在还原数据库之后,......
  • vscode常用快捷键
    1、向上/向下移动代码行alt+下箭头/上箭头2、向上/向下复制一行代码shift+alt+下箭头/上箭头3、选定多个相同的单词Ctrl+d4、全局替换Ctrl+h5、快速定位到某一行Ctrl......
  • css inset属性
    inset属性说明如下:​​inset​​​属性用作定位元素的​​top​​​、​​right​​​、​​bottom​​​、​​left​​​这些属性的简写。类似于​​margin​​​和......
  • JS数组对象 | 中文按照首字母排序sort()、localeCompare()
    一、数组//根据中文の首字母排序letarr=['上海','北京','广州','深圳']arr.sort((a,b)=>a.localeCompare(b))console.log(arr)//数组sort()方法是会改变原数组的,可......
  • el-input聚焦失效Autofocus processing was blocked because a document already has
    问题目前是想实现双击元素时,切换元素,显示出input框,输入新title,失去焦点再切换回去<div@dblclick="editTitle()"><spanv-if="draggable">{{title}}</span><el-input......
  • arcgis api for 自定义zoom
    1.需求自定义UI,实现对地图的zoom操作,在view缩放的时候,带动画效果2.分析问题UI视图一般情况,可能大部分初学者会使用以下代码对zoom进行操作,这个方法是可以放大缩小,但是动画是......
  • Vue3中的异步组件defineAsyncComponentAPI的用法示例
    介绍当我们的项目达到一定的规模时,对于某些组件来说,我们并不希望一开始全部加载,而是需要的时候进行加载;这样的做得目的可以很好的提高用户体验。为了实现这个功能,Vue3中为我......