Array::FindAll<T> Method
Retrieves all the elements that match the conditions defined by the specified predicate.
Assembly: mscorlib (in mscorlib.dll)
public: generic<typename T> static array<T>^ FindAll( array<T>^ array, Predicate<T>^ match )
Type Parameters
- T
The type of the elements of the array.
Parameters
- array
- Type: array<T>
The one-dimensional, zero-based Array to search.
- match
- Type: System::Predicate<T>
The Predicate<T> that defines the conditions of the elements to search for.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is nullptr. -or- match is nullptr. |
The Predicate<T> is a delegate to a method that returns true 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 the elements that match the conditions are saved in the returned array.
This method is an O(n) operation, where n is the Length of array.
The following code example demonstrates the Find<T>, FindLast<T>, and FindAll<T> generic methods. An array of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The code example also defines a search predicate method named EndsWithSaurus, which accepts a string parameter and returns a Boolean value indicating whether the input string ends in "saurus".
The Find<T> generic method traverses the array from the beginning, passing each element in turn to the EndsWithSaurus method. The search stops when the EndsWithSaurus method returns true for the element "Amargasaurus".
Note |
|---|
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 FindLast<T> generic method is used to search the array backward from the end. It finds the element "Dilophosaurus" at position 5. The FindAll<T> generic method is used to return an array containing all the elements that end in "saurus". The elements are displayed.
The code example also demonstrates the Exists<T> and TrueForAll<T> generic methods.
using namespace System; public ref class DinoDiscoverySet { public: static void Main() { array<String^>^ dinosaurs = { "Compsognathus", "Amargasaurus", "Oviraptor", "Velociraptor", "Deinonychus", "Dilophosaurus", "Gallimimus", "Triceratops" }; DinoDiscoverySet^ GoMesozoic = gcnew DinoDiscoverySet(dinosaurs); GoMesozoic->DiscoverAll(); GoMesozoic->DiscoverByEnding("saurus"); } DinoDiscoverySet(array<String^>^ items) { dinosaurs = items; } void DiscoverAll() { Console::WriteLine(); for each(String^ dinosaur in dinosaurs) { Console::WriteLine(dinosaur); } } void DiscoverByEnding(String^ Ending) { Predicate<String^>^ dinoType; if (Ending->ToLower() == "raptor") { dinoType = gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithRaptor); } else if (Ending->ToLower() == "tops") { dinoType = gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithTops); } else if (Ending->ToLower() == "saurus") { dinoType = gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithSaurus); } else { dinoType = gcnew Predicate<String^>(&DinoDiscoverySet::EndsWithSaurus); } Console::WriteLine( "\nArray::Exists(dinosaurs, \"{0}\"): {1}", Ending, Array::Exists(dinosaurs, dinoType)); Console::WriteLine( "\nArray::TrueForAll(dinosaurs, \"{0}\"): {1}", Ending, Array::TrueForAll(dinosaurs, dinoType)); Console::WriteLine( "\nArray::Find(dinosaurs, \"{0}\"): {1}", Ending, Array::Find(dinosaurs, dinoType)); Console::WriteLine( "\nArray::FindLast(dinosaurs, \"{0}\"): {1}", Ending, Array::FindLast(dinosaurs, dinoType)); Console::WriteLine( "\nArray::FindAll(dinosaurs, \"{0}\"):", Ending); array<String^>^ subArray = Array::FindAll(dinosaurs, dinoType); for each(String^ dinosaur in subArray) { Console::WriteLine(dinosaur); } } private: array<String^>^ dinosaurs; // Search predicate returns true if a string ends in "saurus". static bool EndsWithSaurus(String^ s) { if ((s->Length > 5) && (s->Substring(s->Length - 6)->ToLower() == "saurus")) { return true; } else { return false; } } // Search predicate returns true if a string ends in "raptor". static bool EndsWithRaptor(String^ s) { if ((s->Length > 5) && (s->Substring(s->Length - 6)->ToLower() == "raptor")) { return true; } else { return false; } } // Search predicate returns true if a string ends in "tops". static bool EndsWithTops(String^ s) { if ((s->Length > 3) && (s->Substring(s->Length - 4)->ToLower() == "tops")) { return true; } else { return false; } } }; int main() { DinoDiscoverySet::Main(); } /* This code example produces the following output: Compsognathus Amargasaurus Oviraptor Velociraptor Deinonychus Dilophosaurus Gallimimus Triceratops Array.Exists(dinosaurs, "saurus"): True Array.TrueForAll(dinosaurs, "saurus"): False Array.Find(dinosaurs, "saurus"): Amargasaurus Array.FindLast(dinosaurs, "saurus"): Dilophosaurus Array.FindAll(dinosaurs, "saurus"): Amargasaurus Dilophosaurus */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note