C#数组Predicate示例
using System.Collections; public class Class1 { public static void Main() { int[] zArray = { 1, 2, 3, 1, 2, 3, 1, 2, 3 }; Predicate<int> match = new Predicate<int>(MethodA<int>); int[] answers = Array.FindAll(zArray, match); foreach (int answer in answers) { Console.WriteLine(answer); } Console.ReadKey(); } public static bool MethodA<T>(T number) where T : IComparable { int result = number.CompareTo(3); return result == 0; } }
在本文中,我将解释C#中谓词与数组方法的用法。
C# Predicate
C#中的谓词是一个委托(Delegate),它表示定义一组条件并确定指定对象是否满足这些条件的方法。我们可以将此委托与几个数组方法一起使用。它用于搜索集合中的元素。
C# 如何定义delegate
定义委托的语法
public delegate bool Predicate<in T> ( T obj )
日期:2020-04-11 23:03:55 来源:oir作者:oir