C#의 Array.TrueForAll() 메서드는 배열의 모든 요소가 지정된 조건자에 의해 정의된 조건과 일치하는지 여부를 확인하는 데 사용됩니다.
구문
다음은 구문입니다 -
public static bool TrueForAll<T> (T[] array, Predicate<T> match);
예시
이제 Array.TrueForAll() 메서드를 구현하는 예를 살펴보겠습니다. -
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 Ocurrence 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("Eevery element in the array do not matche the conditions defined by the specified predicate");
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Array elements... bike bus Last Ocurrence of element bus is at index = 1 Every element in the array matches the conditions defined by the specified predicate