Array.TrueForAll<T>(T[], Predicate<T>) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Determina se ogni elemento della matrice corrisponde alle condizioni definite dal predicato specificato.
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
Parametri di tipo
- T
Tipo degli elementi della matrice.
Parametri
- array
- T[]
Oggetto unidimensionale in base zero da controllare in base Array alle condizioni.
- match
- Predicate<T>
Predicato che definisce le condizioni da controllare sugli elementi.
Valori restituiti
true se ogni elemento in array corrisponde alle condizioni definite dal predicato specificato; in caso contrario, false. Se nella matrice non sono presenti elementi, il valore restituito è true.
Eccezioni
Esempio
Nell'esempio seguente viene determinato se l'ultimo carattere di ogni elemento di una matrice di stringhe è un numero. Crea due matrici di stringhe. La prima matrice include entrambe le stringhe che terminano con caratteri alfabetici e stringhe che terminano con caratteri numerici. La seconda matrice è costituita solo da stringhe che terminano con caratteri numerici. L'esempio definisce anche un EndWithANumber metodo la cui firma corrisponde al Predicate<T> delegato. L'esempio passa ogni matrice al metodo insieme a TrueForAll un delegato che rappresenta il EndsWithANumber metodo .
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'esempio seguente è simile al primo, ad eccezione del fatto che passa la matrice di stringhe al metodo insieme a TrueForAll un'espressione lambda che determina se un particolare elemento di matrice termina con la rappresentazione di stringa di un numero.
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 entrambi i casi, il TrueForAll metodo restituisce false non appena rileva il primo elemento della matrice che non termina con un numero. In caso contrario, viene restituito true dopo l'iterazione di tutti gli elementi nella matrice.
Note
Come entrambi gli esempi mostrano, in C# e Visual Basic, non è necessario creare in modo esplicito il delegato />
Commenti
Predicate<T> è un delegato a un metodo che restituiscetrue se l'oggetto passato corrisponde alle condizioni definite nel delegato. Gli elementi di array vengono passati singolarmente all'oggetto e l'elaborazione Predicate<T>viene arrestata quando il delegato restituisce false per qualsiasi elemento.
Questo metodo è un'operazione O(n), dove n è l'oggetto Length di array.