📜  C#| Array.Find()方法

📅  最后修改于: 2021-05-29 19:49:00             🧑  作者: Mango

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

句法:

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

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

参数:

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

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

下面的程序说明了Array.Find(T [],Predicate的用法) 方法:

范例1:

// C# program to demonstrate
// Array.Find(T[], Predicate)
// 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)
// 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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.array.find?view=netframework-4.7.2