When i started using the finder in the new generic lists I couldn’t find a good e.g of how to write the find function. So here’s a cut down version of one that i ended up writing.
First I needed a class that could work out if a supplied item was equal to something in my list e.g.
using System;
using System.Collections.Generic;
using System.Text
namespace SoulSolutions.Test
{
///
/// Provides generic functionality to find an object in collection.
///
public class FindObject
{
private int id;
///
/// Constructor
///
/// The unique identifier for the object.
public FindObject(int id)
{
this.id = id;
}
///
/// Predicate to use to find a item in a list based on the
/// id
///
/// The object to compare to
/// true if found/false otherwise
public bool FindByIdPredicate(PersonObject objectData)
{
return objectData.ItemId == id;
}
}
}
Then to use it:
Need to create my FindObject class giving it the value i’m looking for. Then using the list that i want to search, call the find giving it the findobject class with the function that i want to use to find.
List items;
FindObject objectToFind = new FindObject(2);
PersonObject person = items.Find(new Predicate(objectToFind.FindByIdPredicate));