I have a List<> in my program, filled with my custom class. I want to be able to extract an object from the list by simply specifying an integer, then returning all objects that have an integer property set to that integer. I was thinking of doing it like this:
int exampleint = 5;
List<MyClass> extract = new List<MyClass>();
for(int i = 0; i < list.Length; i++) {
if(list[i].Number == exampleint)
extract.Add(list[i]);
}
return extract;
Is there any better or faster way do do this? Just wondering.
Update: Chris, your answer was a little off. This:
List<MyClass> extract = list.FindAll(delegate(int obj) { return obj.Number == exampleint; });
should in fact be this:
List<MyClass> extract = list.FindAll(new Predicate<MyClass>(delegate(MyClass obj) { return obj.Number == exampleint; }));
Your first example gave me errors. But thanks for pointing me in the right direction, it seems to work now.