473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with Generic List FindAll method

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.
Nov 16 '05 #1
3 31427
Michael Rockwell <Mi*************@discussions.microsoft.com> wrote:
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))

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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?

"Jon Skeet [C# MVP]" wrote:
Michael Rockwell <Mi*************@discussions.microsoft.com> wrote:
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))

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Michael Rockwell <Mi*************@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Rockwell | last post by:
I am just starting to use the generics collection objects and am really liking them. I am using the List object and want to use the FindAll method to return a list of items that meets my criteria....
1
by: Omiris | last post by:
I'm trying to use the Equals() method that is defined on the List(Of T) class. The docs seem to state that if T implements the IEquatable interface, then it will use the Equals method that is...
1
by: maxtoroq | last post by:
I know how to work the .FindAll method of the List class, but is there a way to split a list into 2 lists, one containing all the items that match a certain criteria and one that doesn't?
1
by: jappenzeller | last post by:
I have a class that overrides the List<T> generic class and I'm trying to use the FindAll method. I have it working fine, but the method returns a List<T> not a MyList. I guess it's easier to...
7
by: Abhishek | last post by:
Hi I am using a generic list in my program to have a list of objects stored. I want to compare all the objects in this list with object of another class and find all the objects which meet the...
1
by: hardieca | last post by:
Hi, I have a list of sections that contain a parent section ID field that indicates hierarchy throughout a website. I want to filter out all non- root level sections, as indicated by a section...
2
by: Alexnb | last post by:
Okay, I am not sure if there is a better way of doing this than findAll() but that is how I am doing it right now. I am making an app that screen scapes dictionary.com for definitions. However, I...
2
by: Chris Kennedy | last post by:
I am getting a generic list of objects from a webservice. The list is converted to an array by this process. How do I convert the array back to a list and is there any way of finding an object...
3
by: Fresno Bob | last post by:
I have a generic collection of objects and I would like to find the object by one of it's properties e.g. I would like something with the functionality of something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.