473,386 Members | 1,745 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,386 software developers and data experts.

Predicate in Generic Lists

Hello,

I'm using the Generic List class in System.Collection.Generics to
implement a strongly-typed list of my own class. I want to implement the
Find methods and have to use the Predicate delegate to do this.

As I understand it: I create a method which returns a bool and takes in
an object of type <T> where <T> is the type that the generic list was
created on.

Then I call the Find method like so:

myList.Find (new Predicate <T> (myFindMethod));

However, I am unclear as to what exactly myFindMethod should be doing -
what does it compare against or search for? Surely it isn't hard coded
to only find one particular item in the list. So what exactly should it do?

Thanks a lot

Isaac
Aug 21 '05 #1
1 2408
Isaac wrote:
I'm using the Generic List class in System.Collection.Generics to
implement a strongly-typed list of my own class. I want to implement the
Find methods and have to use the Predicate delegate to do this.

As I understand it: I create a method which returns a bool and takes in
an object of type <T> where <T> is the type that the generic list was
created on.

Then I call the Find method like so:

myList.Find (new Predicate <T> (myFindMethod));

However, I am unclear as to what exactly myFindMethod should be doing -
what does it compare against or search for? Surely it isn't hard coded
to only find one particular item in the list. So what exactly should it do?


That's really your problem as the implementor :-) But (more) seriously,
the way I often use the kind of method that takes a delegate is with an
anonymous delegate. Like this:

List<MyObject> myList = ...

public MyObject MyFindMethod(Guid objId) {
return myList.Find(delegate(MyObject obj) {
return obj.Id == objId;
});
}

In this case, the delegate can directly access the value that was passed
in to the method I'm implementing and use it for the comparison.

If you want to implement the delegate as a reusable method, you'll have
to find a way to tell the delegate what exactly it should do when it's
called. One way to do this could be to encapsulate the delegate in a
class, like this:

public MyObjectIdSearcher {
public MyObjectIdSearcher(Guid searchId) {
this.searchId = searchId;
}
private Guid searchId;

public bool PredicateDelegate(MyObject obj) {
return obj.Id == searchId;
}
}

Then your search method might look like this:

public MyObject MyFindMethod(Guid objId) {
return myList.Find(new Predicate<MyObject>(
new MyObjectIdSearcher(objId).PredicateDelegate
);
}

The advantage of the second approach would be that you'd have reusable
encapsulation of the algorithm used to search for a given MyObject by
its Id.

Hope this helps!
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Aug 22 '05 #2

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

Similar topics

3
by: Abhi | last post by:
In the following hypothetical example I want to build a generic list of unique string items. How should I implement the pred function so that it returns true/false if target string exists in the...
2
by: Marco Segurini | last post by:
Hi, I have written the following code: // start code using System; using System.Collections.Generic; using System.Text; namespace MyPredicates
1
by: Isaac | last post by:
Hello, I'm using the Generic List class in System.Collection.Generics to implement a strongly-typed list of my own class. I want to implement the Find methods and have to use the Predicate...
5
by: Andrew Robinson | last post by:
Any easy answer what is wrong here? private List<string> BodyWords = new List<string>(); string word = "Andrew"; the following causes a compilation error:
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
0
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
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...
12
by: Lit | last post by:
Hi, Can someone explain this: <this is from Using generic- By Ludwig Stuyck> string nameToFind = "Ludwig Stuyck"; PersonNameFilter personNameFilter = new PersonNameFilter(nameToFind); //...
5
by: =?Utf-8?B?U2llZ2ZyaWVkIEhlaW50emU=?= | last post by:
Can someone help me convert this to the latest C# syntax using predicate/delegate? You can see my attempt in the comments. Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.