Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Array.FindLast() trong C#

Phương thức Array.FindLast() trong C# tìm kiếm phần tử thỏa mãn điều kiện do predicate chỉ định và trả về lần xuất hiện cuối cùng trong mảng.

Cú pháp

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

Tham số array là mảng một chiều, match là predicate định nghĩa điều kiện tìm kiếm.

Ví dụ

using System;
public class Demo
{
    public static void Main()
    {
        Console.WriteLine("Array elements...");
        string[] arr = { "car", "bike", "truck", "bus" };
        for (int i = 0; i < arr.Length; i++)
        {
            Console.Write("{0} ", arr[i]);
        }
        Console.WriteLine();
        string res = Array.FindLast(arr, ele => ele.StartsWith("b", StringComparison.Ordinal));
        Console.Write("\nLast Occurrence...\n");
        Console.WriteLine("{0}", res);
    }
}

Đầu ra

Array elements... car bike truck bus
Last Occurrence...
bus