C# 中的 Array.FindAll() 方法

csharpserver side programmingprogramming更新于 2025/9/19 12:25:00

C# 中的 Array.FindAll() 方法用于检索符合指定谓词定义的条件的所有元素。

语法

以下是语法 −

public static T[] FindAll<T> (T[] array, Predicate<T> match);

示例

现在让我们看一个实现 Array.FindAll() 方法的示例 −

using System;
public class Demo{
   public static void Main(){
      Console.WriteLine("数组元素...");
      string[] arr = { "car", "bike", "truck", "bus"};
      for (int i = 0; i < arr.Length; i++){
         Console.Write("{0} ", arr[i]);
      }
      Console.WriteLine();
      String[] res = Array.FindAll(arr, ele => ele.StartsWith("b",
      StringComparison.Ordinal));
      Console.Write("
All Searched elements...
");       for (int i = 0; i < res.Length; i++) {          Console.WriteLine("{0}", res[i]);       }       Console.WriteLine();    } }

输出

这将产生以下输出 −

数组元素...
car bike truck bus
All Searched elements...
bike
bus

相关文章