Instead of using Array.Find to find the first matching element in an array, you can use Array.FindAll to find all elements that match a particular set of criteria. If no elements match, FindAll returns null.
Here we search for the Bronte sisters in an array of people:
1
2
3
4
5
6
7
8
| Person[] folks = new Person[4];folks[0] = new Person("Bronte", "Emily");folks[1] = new Person("Bronte", "Charlotte");folks[2] = new Person("Tennyson", "Alfred");folks[3] = new Person("Bronte", "Ernie");// Finds Emily and Charlotte but not ErniePerson[] brontes = Array.FindAll(folks, IsABronteSister); |
Here is the function that looks for a match:
1
2
3
4
5
6
| static bool IsABronteSister(Person p){ string[] firstNames = { "Emily", "Charlotte", "Anne" }; return (p.LastName == "Bronte") && (firstNames.Contains(p.FirstName));} |

