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();
    }
}
相关推荐
??? Meggie44 分钟前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
酷爱码3 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼3 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
喵先生!4 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
Thomas_YXQ4 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics5 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl
程序设计实验室6 小时前
一次小而美的重构:使用 C# 在 Avalonia 中生成真正好看的词云
c#
yuanManGan7 小时前
C++入门小馆: 深入了解STLlist
开发语言·c++
北极的企鹅887 小时前
XML内容解析成实体类
xml·java·开发语言
BillKu7 小时前
Vue3后代组件多祖先通讯设计方案
开发语言·javascript·ecmascript