Help with Generic List FindAll method

G

Guest

I am new to using C# generics and I am liking what I am finding. However the
examples in online help are lacking. Can someone help me with the FindAll
method of the generic List class? As I understand the method, it will return
a list that meets the criteria evaluated in the delegate function that I use
for evaluation. I am not sure how to write this delegate, how do I get the
list item to evaluate in my delegate. Is their a defined delegate signature
or can I define my own. Specifically, I have a list of structures that
contain datapoints at various times and I waht to use FindAll to get a list
of datapoints within a defined timespan. I would send in the begin and end
times and it would return me a list of items in that time range. Any help
would be appreciated.
 
J

Jon Skeet [C# MVP]

Michael Rockwell said:
I am new to using C# generics and I am liking what I am finding. However the
examples in online help are lacking. Can someone help me with the FindAll
method of the generic List class? As I understand the method, it will return
a list that meets the criteria evaluated in the delegate function that I use
for evaluation. I am not sure how to write this delegate, how do I get the
list item to evaluate in my delegate. Is their a defined delegate signature
or can I define my own. Specifically, I have a list of structures that
contain datapoints at various times and I waht to use FindAll to get a list
of datapoints within a defined timespan. I would send in the begin and end
times and it would return me a list of items in that time range. Any help
would be appreciated.

You need to create an implementation of Predicate<T>. For instance:

using System;
using System.Collections.Generic;

class Test
{
static void Main()
{
List<string> x = new List<string>();
x.Add("Hello");
x.Add("There");
x.Add("World");
x.Add("Four");
x.Add("Three");
x.Add("Two");
x.Add("One");

foreach (string s in x.FindAll(TestForLength5))
{
Console.WriteLine (s);
}
}

static bool TestForLength5 (string x)
{
return x.Length==5;
}
}

Note that the call to FindAll could have been written as

x.FindAll(new Predicate<string>(TestForLength5))
 
G

Guest

Thanks, this helps. Can I also pass my own parameters to the Predicate
method so that I can use those parameters in the evaluation. So for example
in your example you have a hard coded length of 5, what if I wanted to pass
the length as a parameter? Can I define my own delegate signature for the
predicate?
 
J

Jon Skeet [C# MVP]

Michael Rockwell said:
Thanks, this helps. Can I also pass my own parameters to the Predicate
method so that I can use those parameters in the evaluation. So for example
in your example you have a hard coded length of 5, what if I wanted to pass
the length as a parameter? Can I define my own delegate signature for the
predicate?

No. The thing to do would be something like:

public class LengthMatcher
{
int length;

public LengthMatcher (int length)
{
this.length;
}

bool Test (string x)
{
return x.Length==length;
}
}

You'd then use something like:

LengthMatcher lm = new LengthMatcher(5);
list.FindAll(lm.Test);

You could also use anonymous types if you wanted - I won't go into
those just now in case it confuses things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top