C# .NET Framework的特殊委托

C# .NET Framework的特殊委托

.NET Framework中定义了几种特殊的委托类型,以简化委托的使用。以下是一些常用的特殊委托类型:

Predicate<T>

这是一个返回布尔值的委托,接受一个类型为T的参数。常用于定义过滤条件。

c# 复制代码
using System;
using System.Collections.Generic;

// 测试一个整数列表,找出所有大于10的整数。
class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 12, 3, 45, 6, 78 };
        Predicate<int> isGreaterThan10 = x => x > 10;

        foreach (var number in numbers.FindAll(isGreaterThan10))
        {
            Console.WriteLine(number);
        }
    }
}

Converter<TInput, TResult>

接受一个类型为TInput的参数,并返回一个类型为TResult的对象。用于类型转换。

c# 复制代码
using System;

// 将字符串数组转换为整数数组。
class Program
{
    static void Main()
    {
        string[] strings = { "1", "2", "3" };
        Converter<string, int> stringToInt = s => int.Parse(s);

        int[] integers = Array.ConvertAll(strings, stringToInt);
        Console.WriteLine(string.Join(", ", integers));
    }
}

Comparison<T>

接受两个类型为T的参数,并返回一个整数,表示它们的相对顺序。常用于排序操作。

c# 复制代码
using System;

class Program
{
    static void Main()
    {
        int[] numbers = { 3, 1, 4, 1, 5 };
        Comparison<int> comparer = (x, y) => x.CompareTo(y);

        Array.Sort(numbers, comparer);
        Console.WriteLine(string.Join(", ", numbers));
    }
}

Action

一个无返回值的委托,可以接受从0到16个参数。Action是其泛型形式。

c# 复制代码
using System;

// 定义一个无参数无返回值的委托,用于打印消息。
class Program
{
    static void PrintMessage()
    {
        Console.WriteLine("Hello, World!");
    }

    static void Main()
    {
        Action printAction = PrintMessage;
        printAction();
    }
}

Func<TResult>

一个有返回值的委托,返回类型为TResult,无参数。Func<T1, T2, ..., TResult>是其泛型形式,可以接受从1到16个参数。

c# 复制代码
using System;

// 定义一个返回布尔值的委托,检查一个数字是否为偶数。
class Program
{
    static void Main()
    {
        Func<int, bool> isEven = number => number % 2 == 0;

        Console.WriteLine(isEven(10)); // 输出 True
        Console.WriteLine(isEven(7));  // 输出 False
    }
}

EventHandler

专门用于事件处理的标准委托类型,接受一个Object类型的sender和EventArgs或其派生类型e作为参数。

c# 复制代码
using System;

// 使用 EventHandler 来定义一个事件处理程序,响应按钮点击事件。
class Program
{
    public event EventHandler ButtonClicked;

    protected virtual void OnButtonClicked(EventArgs e)
    {
        ButtonClicked?.Invoke(this, e);
    }

    public void SimulateButtonClick()
    {
        OnButtonClicked(EventArgs.Empty);
    }
}

class Test
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.ButtonClicked += (sender, e) =>
        {
            Console.WriteLine("Button was clicked.");
        };
        p.SimulateButtonClick();
    }
}
相关推荐
火兮明兮17 分钟前
Python训练第四十三天
开发语言·python
ascarl20101 小时前
准确--k8s cgroup问题排查
java·开发语言
fpcc2 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗2 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长2 小时前
Python 构建法律DeepSeek RAG
开发语言·python
喵叔哟2 小时前
24.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--单体转微服务--认证微服务
微服务·架构·.net
luojiaao2 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码2 小时前
STL解析——list的使用
开发语言·c++
SoFlu软件机器人3 小时前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构
英英_3 小时前
视频爬虫的Python库
开发语言·python·音视频