C#泛型委托(语法糖)

本文介绍了C#中的泛型委托,包括Action委托用于封装无参无返回值的方法,Func委托用于封装带参数并返回指定类型值的方法,以及Predicate委托用于表示判断条件的方法。通过实例展示了如何在实际编程中使用这些委托,如实现ForEach和FindAll操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Action委托

封装一个方法,该方法不具有参数并且不返回值
未使用Action委托:
在这里插入图片描述
使用Action委托:
在这里插入图片描述
使用Action委托写ForEach方法:
标准版:
在这里插入图片描述
简化版:
在这里插入图片描述

Func委托

封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
未使用Func委托:
在这里插入图片描述
使用Func委托:
在这里插入图片描述

Predicate委托

表示定义一组条件并确定指定对象是否符合这些条件的方法。
使用Predicate委托写FindAll方法:
在这里插入图片描述

案例(详细代码)

Frm-05

using Students.BLL;
using Students.Modal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Students
{
   
   
    public partial class Frm_05 : Form
    {
   
   
        StudentBLL studentBLL = new StudentBLL();
        List<StudentModal> studentModalList = new List<StudentModal>();
        public Frm_05()
        {
   
   
            InitializeComponent();
            studentModalList = studentBLL.FindStudentsBySname(new StudentModal());
        }

        private void btn_Action_Click(object sender, EventArgs e)
        {
   
   
            listBox1.Items.Clear();
            //studentModalList.ForEach(new Action<StudentModal>(delegate (StudentModal studentModal)
            //{
   
   
            //    listBox1.Items.Add(studentModal);
            //}));
            MyAction myAction = new MyAction();
            myAction.MyForEach<StudentModal>(studentModalList, listBox1);
        }

        private void btn_Func_Click(object sender, EventArgs e)
        {
   
   
            //var myStudent = new { Name = string.Empty, Age = 0 };
            listBox1.Items.Clear();



            //var newStudentModalList = studentModalList.Select<StudentModal, int>(new Func<StudentModal, int>(delegate (StudentModal studentModal)
            //{
   
   
            //    if (studentModal.Sage > 10)
            //    {
   
   
            //        return studentModal.Sage;
            //    }
            //    return 0;
            //})).ToList();
            //MyAction myAction = new MyAction();
            //myAction.MyForEach<int>(newStudentModalList, listBox1);



            //MyClass
            var newStudentModalList = studentModalList.Select<StudentModal, MyClass>(new Func<StudentModal, MyClass>(delegate (StudentModal studentModal)
            {
   
   
                if (studentModal.Sage > 10)
                {
   
   
                    return new MyClass()
                    {
   
   
                        SName = studentModal.Sname,
                        Sage = studentModal.Sage,
                        Ssex = studentModal.Ssex
                    };
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值