Introduction
Arrays are an integral part of our programming languages. They are data structures that contain a number of variables of the same datatype. Array class has various methods for sorting, searching and manipulating.
When it comes to fetching a matching value from an array or lists, we generally use foreach blocks to iterate through them which not only makes our code long n confusing but also makes us compromise on performance. The System.Array and System.Collections.Generic.List classes of the .NET Framework 2.0 each provide a number of methods that can aid us in getting rid of foreach blocks.
The methods in System. Array class such as Find, FindAll, and FindLast, let us avoid writing code to loop through every element of an array or list to find the one or more items we're looking for. This gives us the ability to "walk" an entire data structure, determining whether each item meets a set of criteria, without having to write the boilerplate code to loop through each row manually.
This is made possible by the use of PREDICATES. Predicate is simply the address of a procedure to call that, in effect, says yea or nay on each item in the collection, you can easily change the search criteria at run time.
Lets examine this through the following simple example.
using System;
public class City
{
public static void Main()
{
// Create an array of string objects.
string[] names = new string[3];
names[0] = "London";
names[1] = "Paris";
names[2] = "Zurich";
// To find the first city name structure which starts with alphabet "P",
// pass the array and a delegate that represents the StartsWithP method
// to the Shared Find method of the Array class.
string city = Array.Find(names, StartsWithP);
// Display the first city found.
Console.WriteLine("City Match Found: {0}", city);
}
// This method implements the test condition for the Find method.
public static bool StartsWithP(string item)
{
return item.StartsWith("P", StringComparison.InvariantCultureIgnoreCase);
}
}
The benefit of using Predicates is that they are more expressive, require less code, promote reuse, and are faster than other alternatives. Using a Predicate turns out to have better performance than a similar foreach operation.
For a thorough knowledge of benefits of Predicates over foreach there is this wonderful article.
http://msmvps.com/blogs/jon.skeet/archive/2006/01/20/foreachperf.aspx

Sanjay DPosted Apr 9, 2007, 7:10 AM
Hi Vandita, Above code snippet not works for me ?? After bit investigation I found following was mistake string[] city; city = Array.Find(names, StartsWithP); should be string city; city = Array.Find(names, StartsWithP); As Array.Find() Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire Array. Thanks a lot. Hmm time 4 me to understand generic If you have good articles pls send me the URLS
vaibhav kadianeditedPosted Feb 21, 2007, 6:09 AMEdited Feb 21, 2007, 7:13 AM
to execute the example, you can do either of the options:- 1) change the city type to string. 2) change Array.Find to Array.FindAll. But you'll only get to see the first found element from the array because display is like that. to display all, you have to change the display also. good and easy example