Array.TrueForAll<T>(T[], Predicate<T>) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Détermine si chaque élément du tableau correspond aux conditions définies par le prédicat spécifié.
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
Paramètres de type
- T
Type des éléments du tableau.
Paramètres
- array
- T[]
Basé sur Array zéro unidimensionnel à vérifier par rapport aux conditions.
- match
- Predicate<T>
Prédicat qui définit les conditions à vérifier sur les éléments.
Retours
true si chaque élément correspondant array aux conditions définies par le prédicat spécifié ; sinon, false. S’il n’existe aucun élément dans le tableau, la valeur de retour est true.
Exceptions
Exemples
L’exemple suivant détermine si le dernier caractère de chaque élément d’un tableau de chaînes est un nombre. Il crée deux tableaux de chaînes. Le premier tableau comprend les deux chaînes qui se terminent par des caractères alphabétiques et des chaînes qui se terminent par des caractères numériques. Le deuxième tableau se compose uniquement de chaînes qui se terminent par des caractères numériques. L’exemple définit également une EndWithANumber méthode dont la signature correspond au Predicate<T> délégué. L’exemple transmet chaque tableau à la TrueForAll méthode avec un délégué qui représente la EndsWithANumber méthode.
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.
L’exemple suivant est similaire au premier, sauf qu’il transmet le tableau de chaînes à la TrueForAll méthode avec une expression lambda qui détermine si un élément de tableau particulier se termine par la représentation sous forme de chaîne d’un nombre.
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.
Dans les deux cas, la TrueForAll méthode retourne false dès qu’elle rencontre le premier élément de tableau qui ne se termine pas par un nombre. Sinon, il retourne true après itération tous les éléments du tableau.
Note
Comme le montrent les deux exemples, en C# et Visual Basic, il n’est pas nécessaire de créer explicitement le délégué Predicate<string> (Predicate(Of String) dans Visual Basic). Ces langages déduitnt le délégué approprié à partir du contexte et créez-le automatiquement.
Remarques
Il Predicate<T> s’agit d’un délégué à une méthode qui retournetrue si l’objet passé à celui-ci correspond aux conditions définies dans le délégué. Les éléments d’entre array eux sont transmis individuellement au Predicate<T>traitement et sont arrêtés lorsque le délégué retourne false pour n’importe quel élément.
Cette méthode est une opération O(n), où n est le Lengtharray.