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();
    }
}
相关推荐
非凡ghost几秒前
MPC-QT视频播放器(基于Qt框架播放器)
开发语言·windows·qt·音视频·软件需求
转基因2 分钟前
C++的IO流
开发语言·c++
一碗绿豆汤4 分钟前
Java语言概述和开发环境-1
java·开发语言
愈努力俞幸运8 分钟前
rust安装
开发语言·后端·rust
天天进步201531 分钟前
【Nanobrowser源码分析4】交互篇: 从指令到动作:模拟点击、滚动与输入的底层实现
开发语言·javascript·ecmascript
console.log('npc')37 分钟前
vue2中子组件父组件的修改参数
开发语言·前端·javascript
码点38 分钟前
【无标题】日文字库Japan.ini
开发语言
IT=>小脑虎42 分钟前
2026版 Python零基础小白学习知识点【基础版详解】
开发语言·python·学习
wjs20241 小时前
抽象工厂模式
开发语言
lly2024061 小时前
SVG 模糊效果详解
开发语言