Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Array.FindAll() ใน C#

เมธอด Array.FindAll() ใน C# ใช้สำหรับดึงองค์ประกอบทั้งหมดที่ตรงกับเงื่อนไขที่กำหนดโดย predicate ที่ระบุ

ไวยากรณ์

ไวยากรณ์มีดังนี้:

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

ตัวอย่าง

ตัวอย่างการใช้งานเมธอด Array.FindAll():

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.FindAll(arr, ele => ele.StartsWith("b", StringComparison.Ordinal)); 
        Console.Write("\nAll Searched elements...\n"); 
        for (int i = 0; i < res.Length; i++) { 
            Console.WriteLine("{0}", res[i]); 
        } 
        Console.WriteLine(); 
    } 
}

ผลลัพธ์

โค้ดจะแสดงผลดังนี้:

Array elements... car bike truck bus 
All Searched elements... 
bike 
bus