此方法用于确定数组中的每个元素是否与指定谓词定义的条件匹配。
用法:
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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。