Array.TrueForAll<T>(T[], Predicate<T>) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Determines whether every element in the array matches the conditions defined by the specified predicate.
public:
generic <typename T>
static bool TrueForAll(cli::array <T> ^ array, Predicate<T> ^ match);
public static bool TrueForAll<T> (T[] array, Predicate<T> match);
static member TrueForAll : 'T[] * Predicate<'T> -> bool
Public Shared Function TrueForAll(Of T) (array As T(), match As Predicate(Of T)) As Boolean
- T
The type of the elements of the array.
- array
- T[]
The one-dimensional, zero-based Array to check against the conditions.
- match
- Predicate<T>
The predicate that defines the conditions to check against the elements.
true
if every element in array
matches the conditions defined by the specified predicate; otherwise, false
. If there are no elements in the array, the return value is true
.
The following example determines whether the last character of each element in a string array is a number. It creates two string arrays. The first array includes both strings that end with alphabetic characters and strings that end with numeric characters. The second array consists only of strings that end with numeric characters. The example also defines an EndWithANumber
method whose signature matches the Predicate<T> delegate. The example passes each array to the TrueForAll method along with a delegate that represents the EndsWithANumber
method.
using System;
public class Example
{
public static void Main()
{
String[] values1 = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" };
String[] values2 = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" };
if (Array.TrueForAll(values1, EndsWithANumber))
Console.WriteLine("All elements end with an integer.");
else
Console.WriteLine("Not all elements end with an integer.");
if (Array.TrueForAll(values2, EndsWithANumber))
Console.WriteLine("All elements end with an integer.");
else
Console.WriteLine("Not all elements end with an integer.");
}
private static bool EndsWithANumber(string value)
{
int s;
return int.TryParse(value.Substring(value.Length - 1), out s);
}
}
// The example displays the following output:
// Not all elements end with an integer.
// All elements end with an integer.
open System
let endsWithANumber (value: string) =
value.Substring(value.Length - 1)
|> Int32.TryParse
|> fst
let values1 = [| "Y2K"; "A2000"; "DC2A6"; "MMXIV"; "0C3" |]
let values2 = [| "Y2"; "A2000"; "DC2A6"; "MMXIV_0"; "0C3" |]
if Array.TrueForAll(values1, endsWithANumber) then
printfn "All elements end with an integer."
else
printfn "Not all elements end with an integer."
if Array.TrueForAll(values2, endsWithANumber) then
printfn "All elements end with an integer."
else
printfn "Not all elements end with an integer."
// The example displays the following output:
// Not all elements end with an integer.
// All elements end with an integer.
Module Example
Public Sub Main()
Dim values1() As String = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" }
Dim values2() As String = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" }
If Array.TrueForAll(values1, AddressOf EndsWithANumber) Then
Console.WriteLine("All elements end with an integer.")
Else
Console.WriteLine("Not all elements end with an integer.")
End If
If Array.TrueForAll(values2, AddressOf EndsWithANumber) Then
Console.WriteLine("All elements end with an integer.")
Else
Console.WriteLine("Not all elements end with an integer.")
End If
End Sub
Private Function EndsWithANumber(value As String) As Boolean
Dim s As Integer
Return Int32.TryParse(value.Substring(value.Length - 1), s)
End Function
End Module
' The example displays the following output:
' Not all elements end with an integer.
' All elements end with an integer.
The following example is similar to the first, except that it passes the string array to the TrueForAll method along with a lambda expression that determines whether a particular array element ends with the string representation of a number.
using System;
public class Example
{
public static void Main()
{
String[] values = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" };
if (Array.TrueForAll(values, value => {
int s;
return int.TryParse(value.Substring(value.Length - 1), out s); }
))
Console.WriteLine("All elements end with an integer.");
else
Console.WriteLine("Not all elements end with an integer.");
}
}
// The example displays the following output:
// Not all elements end with an integer.
open System
let values = [| "Y2K"; "A2000"; "DC2A6"; "MMXIV"; "0C3" |]
if Array.TrueForAll(values,
fun value ->
value.Substring(value.Length - 1)
|> Int32.TryParse
|> fst) then
printfn "All elements end with an integer."
else
printfn "Not all elements end with an integer."
// The example displays the following output:
// Not all elements end with an integer.
Module Example
Public Sub Main()
Dim values() As String = { "Y2K", "A2000", "DC2A6", "MMXIV", "0C3" }
'Dim values2() As String = { "Y2", "A2000", "DC2A6", "MMXIV_0", "0C3" }
If Array.TrueForAll(values, Function(value)
Dim s As Integer
Return Int32.TryParse(value.Substring(value.Length - 1), s)
End Function) Then
Console.WriteLine("All elements end with an integer.")
Else
Console.WriteLine("Not all elements end with an integer.")
End If
End Sub
End Module
' The example displays the following output:
' Not all elements end with an integer.
In both cases, the TrueForAll method returns false
as soon as it encounters the first array element that does not end in a number. Otherwise, it returns true
after iterating all the elements in the array.
Notes
As both examples show, in C# and Visual Basic, it is not necessary to create the Predicate<string>
delegate (Predicate(Of String)
in Visual Basic) explicitly. These languages infer the correct delegate from context and create it automatically.
The Predicate<T> is a delegate to a method that returnstrue
if the object passed to it matches the conditions defined in the delegate. The elements of array
are individually passed to the Predicate<T>, and processing is stopped when the delegate returns false
for any element.
This method is an O(n
) operation, where n
is the Length of array
.
Produit | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :