了解.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;
    }
}
相关推荐
datalover3 小时前
【无标题】
c#·linq
FlYFlOWERANDLEAF5 小时前
微软CommunityToolkit.Mvvm和DevExpress中GenerateViewModel使用
c#
人丰5 小时前
血泪复盘:一次由 HttpContext.Current 引发的 CPU 100% 惨案
性能优化·.net
Xin_ye100866 小时前
第七章:进阶篇——事件监听、外部服务集成与性能优化
c#
z落落6 小时前
C# WinForm NModbus 串口通信+WinForm Modbus4 串口通信
开发语言·单片机·c#
CoderIsArt6 小时前
C#环境中部署MQTT客户端
开发语言·c#
CSDN_RTKLIB7 小时前
模型空间、布局控件、自定义块
c#
z落落8 小时前
C# WinForm TCP 服务器+WinForm TCP 客户端
服务器·tcp/ip·c#
csdn_aspnet8 小时前
C# 多个串口多个线程发送数据和接收数据
c#·串口·thread·com·serialport
ChaITSimpleLove8 小时前
用 .NET 10 技术栈 “玩转 AI 学习” 的路线图:从 Minimal API 到 AI Agent 产品
人工智能·.net·学习 ai