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


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


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

用法:

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

参数:


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

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

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

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

示例1:

// C# program to demonstrate 
// Array.FindLast(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", "Son", "Tue", "Thu"}; 
  
            // Display the values of the myArr. 
            Console.WriteLine("Initial Array:"); 
  
            // calling the PrintIndexAndValues() 
            // method to print 
            PrintIndexAndValues(myArr); 
  
            // getting a last element with required 
            // condition using method FindLast() 
            string value = Array.FindLast(myArr,  
                   element => element.StartsWith("S", 
                          StringComparison.Ordinal)); 
  
            // Display the value of 
            // the found element. 
            Console.Write("Last occurrence: "); 
  
            // 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
Son
Tue
Thu

Last occurrence: Son

示例2:

// C# program to demonstrate 
// Array.FindLast(T[], Predicate<T>) 
// Method 
using System; 
using System.Collections.Generic; 
  
public class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
  
        try { 
  
            // Creating and intializing 
            // new Array String with null 
            String[] myArr = null; 
  
            // getting a last element with required 
            // condition using method FindLast() 
            string value = Array.FindLast(myArr, 
                element => element.StartsWith("S", 
                       StringComparison.Ordinal)); 
  
            // Display the value of 
            // the found element. 
            Console.Write("Last occurrence: "); 
  
            // 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.FindLast() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。