List<T>::Exists Method (Predicate<T>^)
Determines whether the List<T> contains elements that match the conditions defined by the specified predicate.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- match
-
Type:
System::Predicate<T>^
The Predicate<T> delegate that defines the conditions of the elements to search for.
Return Value
Type: System::Booleantrue if the List<T> contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | match is null. |
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 the current List<T> are individually passed to the Predicate<T> delegate, and processing is stopped when a match is found.
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
The following example demonstrates the Exists method and several other methods that use the Predicate<T> generic delegate.
A List<T> of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The 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, FindLast, and FindAll methods are used to search the list with the search predicate method, and then the RemoveAll method is used to remove all entries ending with "saurus".
Finally, the Exists method is called. It traverses the list from the beginning, passing each element in turn to the EndsWithSaurus method. The search stops and the method returns true if the EndsWithSaurus method returns true for any element. The Exists method returns false because all such elements have been removed.
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. |
using namespace System; using namespace System::Collections::Generic; // Search predicate returns true if a string ends in "saurus". bool EndsWithSaurus(String^ s) { return s->ToLower()->EndsWith("saurus"); }; void main() { List<String^>^ dinosaurs = gcnew List<String^>(); dinosaurs->Add("Compsognathus"); dinosaurs->Add("Amargasaurus"); dinosaurs->Add("Oviraptor"); dinosaurs->Add("Velociraptor"); dinosaurs->Add("Deinonychus"); dinosaurs->Add("Dilophosaurus"); dinosaurs->Add("Gallimimus"); dinosaurs->Add("Triceratops"); Console::WriteLine(); for each(String^ dinosaur in dinosaurs ) { Console::WriteLine(dinosaur); } Console::WriteLine("\nTrueForAll(EndsWithSaurus): {0}", dinosaurs->TrueForAll(gcnew Predicate<String^>(EndsWithSaurus))); Console::WriteLine("\nFind(EndsWithSaurus): {0}", dinosaurs->Find(gcnew Predicate<String^>(EndsWithSaurus))); Console::WriteLine("\nFindLast(EndsWithSaurus): {0}", dinosaurs->FindLast(gcnew Predicate<String^>(EndsWithSaurus))); Console::WriteLine("\nFindAll(EndsWithSaurus):"); List<String^>^ sublist = dinosaurs->FindAll(gcnew Predicate<String^>(EndsWithSaurus)); for each(String^ dinosaur in sublist) { Console::WriteLine(dinosaur); } Console::WriteLine( "\n{0} elements removed by RemoveAll(EndsWithSaurus).", dinosaurs->RemoveAll(gcnew Predicate<String^>(EndsWithSaurus))); Console::WriteLine("\nList now contains:"); for each(String^ dinosaur in dinosaurs) { Console::WriteLine(dinosaur); } Console::WriteLine("\nExists(EndsWithSaurus): {0}", dinosaurs->Exists(gcnew Predicate<String^>(EndsWithSaurus))); } /* This code example produces the following output: Compsognathus Amargasaurus Oviraptor Velociraptor Deinonychus Dilophosaurus Gallimimus Triceratops TrueForAll(EndsWithSaurus): False Find(EndsWithSaurus): Amargasaurus FindLast(EndsWithSaurus): Dilophosaurus FindAll(EndsWithSaurus): Amargasaurus Dilophosaurus 2 elements removed by RemoveAll(EndsWithSaurus). List now contains: Compsognathus Oviraptor Velociraptor Deinonychus Gallimimus Triceratops Exists(EndsWithSaurus): False */
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
