首页 > 编程语言 >[C#] Func、Action的简单使用

[C#] Func、Action的简单使用

时间:2022-12-02 18:55:05浏览次数:36  
标签:Test1 return C# void List funcTest Func Action data

参考代码:

using System;
using System.Collections.Generic;
using System.Linq;

namespace FuncActionDemo
{
    class FuncActionTest
    {
        public void Test1(List<int> data,Func<int,bool> filter)
        {
            var result = data.Where(x => filter(x)).ToList();
            result.ForEach(x =>
            {
                Console.WriteLine(x);
            }
            );

        }
        public void Test2(List<int> data,Action<int, int>action)
        {
            var d = data.OrderBy(x => x).ToList();
            action(d[0], d[1]);
         
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FuncActionTest funcTest = new FuncActionTest();
            List<int> data = new List<int> { 1, 2, 4, 7, 8, 9 };
            funcTest.Test1(data, x => {
                if (x % 2 == 0)
                    return true;
                else
                    return false;
            });
            funcTest.Test1(data, x => { return FilterEven(x); });
            funcTest.Test1(data, FilterEven);
            Console.WriteLine("-----------");
            funcTest.Test2(data,Add);
        }
        static bool FilterEven(int s)
        {
            if (s % 2 == 0)
                return true;
            else
                return false;
        }
        static void Add( int a, int b)
        {
            Console.WriteLine(a + b);
        }
    }
}

 

标签:Test1,return,C#,void,List,funcTest,Func,Action,data
From: https://www.cnblogs.com/exesoft/p/16945378.html

相关文章

  • css基础操作2
    今日内容总结边框/*border-left-width:边框大小(px);*//*border-left-style:边框格式;*//*border-left-color:颜色选择*/简写:border-left:边框大小边框格式......
  • 题解【CF1592F2 Alice and Recoloring 2】
    CF1592F2AliceandRecoloring2解题报告。不一定更好的阅读体验。摘自我的构造题目选做例题IV。CF2800的构造就这?/cf/cf/cf(首先,操作2和操作3都是没有用......
  • Css3 将网页变成黑白_Css3 网页黑白滤镜filter
    一、Css3将网页变成黑白_Css3网页黑白滤镜filter重点代码:html,body{-webkit-filter:grayscale(100%);-moz-filter:gr......
  • mysql case when then用法(转)
    mysqlcasewhenthen用法(转)原文:https://www.cnblogs.com/Richardzhu/p/3571670.htmlcase具有两种格式。简单case函数和case搜索函数。--简单case函数casesexwhe......
  • taosBenchmark测试
    taosBenchmark测试使用taosBenchmark体验写入速度使用TDengineCLI体验查询速度dockerexec-itcd849a7764fc/bin/bashtaos>selectcount(*)fromtest.meters;......
  • 分享45款高质量的免费(X)HTML/CSS模板
    ​​GreenPlanet​​(​​演示​​​|​​下载​​)​​​​​​CreativeStudio​​(​​演示​​​|​​下载​​)​​​​​​RestaurantWebsite​​(​​演示......
  • Python使用GARCH,EGARCH,GJR-GARCH模型和蒙特卡洛模拟进行股价预测|附代码数据
    全文下载链接:http://tecdat.cn/?p=20678在本文中,预测股价已经受到了投资者,政府,企业和学者广泛的关注。然而,数据的非线性和非平稳性使得开发预测模型成为一项复杂而具有挑......
  • sourcetree和git无法识别新增文件
     在工程中新建文件,但是git和sourcetree无法识别,我是用的是Xcode添加的文件和图片,全都无法识别。例如,新建一个类文件,.h和.m都是别不出来,但是工程文件显示已经添加相对应......
  • C#图片处理基本应用(裁剪,缩放,清晰度,水印)
    C#图片处理基本应用(裁剪,缩放,清晰度,水印)前言需求源自项目中的一些应用,比如相册功能,通常用户上传相片后我们都会针对该相片再生成一张缩略图,用于其它页面上的列表显示。......
  • docker常用
    docker镜像网址https://hub.docker.com/search?q=&type=imagehttps://hub.docker.com/后续......