After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class)
For example:
public class Car
{
public string Make;
public string Model;
public int Year;
}
{ // somewhere in my code
List<Car> carList = new List<Car>();
// ... code to add Cars ...
Car myCar = new Car();
// Find the first of each car made between 1980 and 2000
for (int x = 1980; x < 2000; x++)
{
myCar = carList.Find(byYear(x));
Console.Writeline(myCar.Make + myCar.Model);
}
}
What should my "byYear" predicate look like?
(The MSDN example only talks about a List of dinosaurs and only searches for an unchanging value "saurus" -- It doesn't show how to pass a value into the predicate...)
EDIT: I'm using VS2005/.NET2.0, so I don't think Lambda notation is available to me...
EDIT2: Removed "1999" in the example because I may want to "Find" programatically based on different values. Example changed to range of cars from 1980 to 2000 using for-do loop.
You can use a lambda expression as follows:
You can use this too:
Or you can use an anonymous delegate:
Ok, in .NET 2.0 you can use delegates, like so:
Since you can't use lambda you can just replace it with an anonymous delegate.
Hmm. Thinking more about it, you could use currying to return a predicate.
Now you can pass the result of this function (which is a predicate) to your Find method: