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


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


此方法用于搜索与指定谓词定义的条件匹配的元素,并返回整个Array中的第一个匹配项。

用法:

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

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


参数:

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

返回值:如果找到,则此方法返回与指定谓词定义的条件匹配的第一个元素。否则,它将返回类型T的默认值。

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

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

示例1:

// C# program to demonstrate 
// Array.Find(T[], Predicate<T>) 
// 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", "Thu"}; 
  
            // 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.Find(myArr,  
                       element => element.StartsWith("S", 
                       StringComparison.Ordinal)); 
  
            // Display the value of  
            // the found element. 
            Console.Write("Element: "); 
  
            // printing the string  
            // following the condition 
            Console.Write("{0}", 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
Thu

Element: Sun

示例2:

// C# program to demonstrate 
// Array.Find(T[], Predicate<T>) 
// Method  
using System; 
using System.Collections.Generic; 
  
public class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try { 
  
            // Creating and initializing new  
            // the String with the null value 
            String[] myArr = null; 
  
            // getting an element a with  
            // required condition 
            // using method Find() 
            string value = Array.Find(myArr,  
                    element => element.StartsWith("S",  
                    StringComparison.Ordinal)); 
  
            // Display the value of  
            // the found element. 
            Console.Write("Element: "); 
  
            // printing the string  
            // following the condition 
            Console.Write("{0}", 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(); 
    } 
}
输出:
Exception Thrown: System.ArgumentNullException

参考:



相关用法


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