Phương thức Array.TrueForAll() trong C# xác định xem mọi phần tử trong mảng có thỏa mãn điều kiện do predicate chỉ định hay không.
Cú pháp
public static bool TrueForAll(T[] array, Predicate match);
Ví dụ
using System;
public class Demo
{
public static void Main()
{
Console.WriteLine("Array elements...");
string[] arr = { "bike", "bus" };
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.WriteLine();
int lastIndex = Array.LastIndexOf(arr, "bus");
Console.WriteLine("Last Occurrence of element bus is at index = " + lastIndex);
bool res = Array.TrueForAll(arr, ele => ele.StartsWith("b", StringComparison.Ordinal));
if (res)
Console.Write("Every element in the array matches the conditions defined by the specified predicate.");
else
Console.Write("Not every element in the array matches the conditions defined by the specified predicate");
}
}
Đầu ra
Array elements... bike bus Last Occurrence of element bus is at index = 1 Every element in the array matches the conditions defined by the specified predicate