了解.NET Framework中自带的泛型委托Predicate和Comparison

Predicate表示定义一组条件并确定指定对象是否符合这些条件的方法。Comparison表示比较同一类型的两个对象的方法。本文主要介绍.NET Framework中自带的泛型委托Predicate和Comparison的使用。

1、Predicate

Predicate 相当于 FuncAction类似的委托。表示定义一组条件并确定指定对象是否符合这些条件的方法。Predicate委托通常由 ArrayList<T> 类的几种方法使用,常用于在集合中搜索元素。

.NET Framework中的定义如下,

public delegate bool Predicate<in T>(T obj)

例如,

复制代码
using System;
using System.Reflection;
using System.Collections.Generic;
namespace Predicate
{
    public struct Point
    {
      public Point(int x,int y)
      {
        this.X = x;
        this.Y = y;
      }
      public int X { get;set;}
      public int Y { get;set;}
    }
    class Program
    {
        static void Main(string[] args)
        {
            Point[] points =
                {
                new Point(100,200),
                new Point(150,250),
                new Point(250,375),
                new Point(275,390),
                new Point(296,400)
            };
            Predicate<Point> predicate = FindPoints;
            Point first1 = Array.Find(points, predicate);
            Console.WriteLine("使用FindPoints:");
            Console.WriteLine($"Found:X={first1.X},Y={first1.Y}");
            Point first2 = Array.Find(points, ptn => ptn.X * ptn.Y > 100000);
            Console.WriteLine("使用Lambda:");
            Console.WriteLine($"Found:X={first2.X},Y={first2.Y}");
            Console.ReadKey();
        }
        private static bool FindPoints(Point ptn)
        {
            return ptn.X * ptn.Y > 100000;
        }
    }
}

2、Comparison

Comparison委托由 Array 类的 Sort<T>(T[], Comparison<T>)方法重载和 List<T> 类的 Sort(Comparison<T>) 方法重载使用,用于对数组或列表中的元素进行排序。

.NET Framework中的定义如下,

public delegate int Comparison<in T>(T x, T y)

例如,

复制代码
using System;
class ListSort
{
    static void Main()
    {
        int[] nums = {3,6,8,1,2,9}; 
        //使用匿名方法实现 Comparison 
        Array.Sort(nums , delegate(int i,int j){  
                       if (i == j)                       // 这个接口的返回值为 1 0 -1. 用来实现排序
                       {                               // 这里是倒序排列
                           return 0;    //相等 返回 0
                       }
                       else if (i < j)
                       {
                           return 1;    
                       }
                       else
                           return -1;
                   });
        foreach(int i in nums)
            Console.Write(i+",");
        Console.WriteLine();
        nums=new int[] {13,16,18,11,12,19}; 
        //使用lambda实现 Comparison 
        Array.Sort(nums , (int i,int j)=>{  //使用匿名方法实现 Comparison 
                       if (i == j)                       // 这个接口的返回值为 1 0 -1. 用来实现排序
                       {                               // 这里是倒序排列
                           return 0;    //相等 返回 0
                       }
                       else if (i < j)
                       {
                           return 1;    
                       }
                       else
                           return -1;
                   });
        foreach(int i in nums)
            Console.Write(i+",");
        Console.WriteLine();
        nums=new int[] {23,26,28,21,22,29}; 
        //使用定义方法实现 Comparison 
        Array.Sort(nums , CompareValue);
        #region output
        foreach(int i in nums)
            Console.Write(i+",");
        Console.WriteLine();
        #endregion
    }
    private static int CompareValue(int i,int j)
    {
       if (i == j)                       // 这个接口的返回值为 1 0 -1. 用来实现排序
       {                               // 这里是倒序排列
           return 0;    //相等 返回 0
       }
       else if (i < j)
       {
           return 1;    
       }
       else
           return -1;
    }
}
相关推荐
淡海水39 分钟前
【原理】Struct 和 Class 辨析
开发语言·c++·c#·struct·class
淡海水42 分钟前
【原理】Unity GC 对比 C# GC
unity·c#·gc·垃圾回收
张人玉1 小时前
C#读取文件, IO 类属性及使用示例
microsoft·c#
许泽宇的技术分享2 小时前
Windows桌面自动化的革命性突破:深度解析Windows-MCP.Net Desktop模块的技术奥秘
windows·自动化·.net
咕白m6256 小时前
通过 C# 高效提取 PDF 文本的完整指南
后端·c#
hqwest9 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple10 小时前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
追逐时光者21 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 50 期(2025年8.11-8.17)
后端·.net
★YUI★1 天前
学习游戏制作记录(玩家掉落系统,删除物品功能和独特物品)8.17
java·学习·游戏·unity·c#
谷宇.1 天前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程