当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Array.FindAll()用法及代码示例


此方法用于检索与指定谓词定义的条件匹配的所有元素。

用法:

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

在此,T是数组元素的类型。


参数:

  • array: 它是要搜索的一维,从零开始的数组。
  • match: 谓词定义了要搜索的元素的条件。

返回值:如果找到该方法,则此方法返回一个包含所有与指定谓词定义的条件相匹配的元素的数组。否则,它将返回一个空数组。

异常:如果数组为null或match为null,则此方法引发ArgumentNullException。

以下示例程序旨在说明Array.FindAll(T [],Predicate)方法的用法:

示例1:

// C# program to demonstrate 
// FindAll() method 
using System; 
using System.Collections.Generic; 
  
public class GFG { 
  
// Main Method 
public static void Main() 
{ 
  
    try { 
  
        // Creating and intializing new the String 
        String[] myArr = {"Sun", "Mon", "Tue", "Sat"}; 
  
        // Display the values of the myArr. 
        Console.WriteLine("Initial Array:"); 
  
        // calling the PrintIndexAndValues() 
        // method to print 
        PrintIndexAndValues(myArr); 
  
        // getting a element a with required  
        // condition using method Find() 
        String[] value = Array.FindAll(myArr,  
               element => element.StartsWith("S",  
               StringComparison.Ordinal)); 
  
        // Display the value  
        // of the found element. 
        Console.WriteLine("Elements are: "); 
  
        // printing the Array of String 
        PrintIndexAndValues(value); 
    } 
    catch (ArgumentNullException e) { 
  
        Console.Write("Exception Thrown: "); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining the method 
// PrintIndexAndValues 
public static void PrintIndexAndValues(String[] myArr) 
{ 
    for (int i = 0; i < myArr.Length; i++) { 
  
        Console.WriteLine("{0}", myArr[i]); 
    } 
    Console.WriteLine(); 
} 
}
输出:
Initial Array:
Sun
Mon
Tue
Sat

Elements are: 
Sun
Sat

示例2:

// C# program to demonstrate 
// FindAll() method 
// For ArgumentNullException 
using System; 
using System.Collections.Generic; 
  
public class GFG { 
  
// Main Method 
public static void Main() 
{ 
  
    try { 
  
        // Creating and initializing 
        // new the String 
        String[] myArr = null; 
  
        // getting a element a with  
        // required condition using 
        // method Find() 
        Console.WriteLine("Trying to get the element from a null array"); 
        Console.WriteLine(); 
        String[] value = Array.FindAll(myArr,  
                  element => element.StartsWith("S", 
                  StringComparison.Ordinal)); 
  
        // Display the value of the found element. 
        Console.WriteLine("Elements are: "); 
  
        // printing the Array of String 
        PrintIndexAndValues(value); 
    } 
  
    catch (ArgumentNullException e) { 
  
        Console.Write("Exception Thrown: "); 
        Console.Write("{0}", e.GetType(), e.Message); 
    } 
} 
  
// Defining the method 
// PrintIndexAndValues 
public static void PrintIndexAndValues(String[] myArr) 
{ 
    for (int i = 0; i < myArr.Length; i++) { 
  
        Console.WriteLine("{0}", myArr[i]); 
    } 
    Console.WriteLine(); 
} 
}
输出:
Trying to get the element from a null array

Exception Thrown: System.ArgumentNullException

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Array.FindAll() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。