此方法用於確定數組中的每個元素是否與指定謂詞定義的條件匹配。
用法:
public static bool TrueForAll (T[] array, Predicate<T> match);
在此,T是數組元素的類型。
參數:
-
array:它是一維,從零開始的數組,用於檢查條件。
match:謂詞定義了檢查元素的條件。
返回值:如果數組中的每個元素都匹配指定謂詞定義的條件,則此方法返回true,否則返回false。如果數組中沒有元素,則返回值為true。
異常:如果數組為null或match為null,則此方法拋出ArgumentNullException。
以下示例程序旨在說明Array.TrueForAll(T [],Predicate)方法的用法:
示例1:
// C# program to demonstrate
// TrueForAll() 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", "Sue", "Shu"};
// Display the values of the myArr.
Console.WriteLine("Initial Array:");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(myArr);
// getting the bool value
// with required condition
// using method TrueForAll()
bool value = Array.TrueForAll(myArr, element => element.StartsWith("S",
StringComparison.Ordinal));
// Checking the condition
if (value)
Console.Write("Every Element is satisfying condition");
else
Console.Write("Every Element is not satisfying condition");
}
catch (ArgumentException 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 Sue Shu Every Element is satisfying condition
示例2:對於ArgumentNullException
// C# program to demonstrate
// TrueForAll() method
// For ArgumentNullException
using System;
using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Creating and initializing new
// the String with a null value
String[] myArr = null;
// getting the bool value
// with required condition
// using method TrueForAll()
Console.WriteLine("Trying to get the boolean "
+"value while myArr is null");
Console.WriteLine();
bool value = Array.TrueForAll(myArr, element => element.StartsWith("S",
StringComparison.Ordinal));
// Checking the condition
if (value)
Console.Write("Every Element is satisfying condition");
else
Console.Write("Every Element is not satisfying condition");
}
catch (ArgumentException 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();
}
}
輸出:
Trying to get the boolean value while myArr is null Exception Thrown: System.ArgumentNullException
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Array.TrueForAll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。