473,500 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is a "Predicate delegate"

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?

Thanks.
Mar 29 '06 #1
8 78067
Jeff S. <A@B.COM> wrote:
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?


It's an implementation of the generic System.Predicate<T> delegate.

See http://msdn2.microsoft.com/en-us/lib...bz(VS.80).aspx for
details...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 29 '06 #2
http://msdn2.microsoft.com/en-us/lib...bz(VS.80).aspx

"Jeff S." <A@B.COM> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
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?

Thanks.

Mar 29 '06 #3
A delegate is like a function pointer. A Predicate delegate is a very cool
use of Delegates which is used with various Generic Lists, Arrays and
Collections. Since a Generic List does not specify a data type, that is, the
data type of the items in the List is set with a type parameter, how would
one write a Find() function for a Generic List? Since nothing is known about
the data type being searched for, one cannot know what sort of condition is
used to discriminate between one item and another.

The Predicate delegate is a Generic method which takes an object of type T
as a parameter. It returns true or false, indicating whether or not the
object of type T satisfies the condition it tests. The Predicate delegate is
assigned the same type as the List when the List type parameter is passed to
the instance of the Generic List created. The author of the code knows what
the data type is, and can write a method that works with the type passed to
test it and see whether it is whatever the author wants it to be. The Find()
method uses the Predicate delegate method created to test each item in the
List, and returns the first item in the List for which the Predicate
delegate returns true.

As an example, let's say that we have a Generic List of type Int32:

private List<int> Numbers = new List<int>();

You want to find the first item in the List that is less than 5. So, you
create your method:

private static bool IsLessThanFive(int i)
{
return (i < 5);
}

So, now you call the Find() method:

int result = Numbers.Find(new Predicate<int>(IsLessThanFive);

The IsLessThanFive Predicate delegate is called for each item in the list,
passing that item as the parameter, until one of the calls returns true.
That is the number that is assigned to result.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
"Jeff S." <A@B.COM> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
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?

Thanks.

Mar 29 '06 #4
A predicate is just a method that returns true or false. In the case of
List<T>, you can pass it a delegate that is your code block to return true
or false.
You can do the same thing yourself without predicates. For example here is
a collection that uses both to give you a basic idea:

public class Customers

{

private List<Person> list;

public Customers()

{

list = new List<Person>();

}

// Prior to List<T>, you would do something like this. Can still do.

public Person FindName(string name)

{

lock (list)

{

foreach (Person p in list)

{

if (p.Name == name)

return p;

}

return null;

}

}

// Passing delegate to List.Find() in 2.0.

public Person FindName2(string name)

{

lock (list)

{

// Find will enumerate the list and eval the predicate for each
item and return first item that returns true.

return list.Find(delegate(Person p)

{

return p.Name == name;

});

}

}

}

public class Person

{

public string Name;

public int Age;

}
Both methods return same result, You can decide if predicates are helpful
or not.

--
William Stacey [MVP]

"Jeff S." <A@B.COM> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
|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?
|
| Thanks.
|
|
Mar 29 '06 #5
Added another method to round out the example. Find3() takes a delegate we defined. This is how you might do the same thing before Predicate<T>.

public delegate bool PersonDel(Person p);

public class Customers

{

private List<Person> list;

public Customers()

{

list = new List<Person>();

}

public void Add(Person p)

{

if (p == null)

throw new ArgumentNullException("p");

lock (list)

{

list.Add(p);

}

}

// Prior to List<T>, you would do something like this. Can still do.

public Person Find(string name)

{

lock (list)

{

foreach (Person p in list)

{

if (p.Name == name)

return p;

}

return null;

}

}

// Passing delegate to List.Find() in 2.0.

public Person Find2(string name)

{

lock (list)

{

// Find will enumerate the list and eval the predicate for each item and return first item that returns true.

return list.Find(delegate(Person p)

{

return p.Name == name;

});

}

}

// Using your own delegate type to simulate what Predicate<T> does for you.

public Person Find3(PersonDel del)

{

lock (list)

{

foreach (Person p in list)

{

if ( del(p))

return p;

}

return null;

}

}

public static void Test()

{

Customers c = new Customers();

c.Add(new Person("Amy", 30));

c.Add(new Person("Joe", 35));

Person p = c.Find("Amy");

Console.WriteLine(p.ToString());

p = c.Find2("Joe");

Console.WriteLine(p.ToString());

p = c.Find3(delegate(Person person)

{

return person.Name == "Amy";

});

Console.WriteLine(p.ToString());

}

}

public class Person

{

public string Name;

public int Age;

public Person(string name, int age)

{

this.Name = name;

this.Age = age;

}

public override string ToString()

{

return Name + ", " + Age;

}

}
--
William Stacey [MVP]

Mar 29 '06 #6
Kevin,

Excellent description. In addition you can use anonymous methods and
delegate inference to write it all in one line of code.

int result = Numbers.Find(delegate(int i) { return i < 5; });

Brian

Kevin Spencer wrote:
A delegate is like a function pointer. A Predicate delegate is a very cool
use of Delegates which is used with various Generic Lists, Arrays and
Collections. Since a Generic List does not specify a data type, that is, the
data type of the items in the List is set with a type parameter, how would
one write a Find() function for a Generic List? Since nothing is known about
the data type being searched for, one cannot know what sort of condition is
used to discriminate between one item and another.

The Predicate delegate is a Generic method which takes an object of type T
as a parameter. It returns true or false, indicating whether or not the
object of type T satisfies the condition it tests. The Predicate delegate is
assigned the same type as the List when the List type parameter is passed to
the instance of the Generic List created. The author of the code knows what
the data type is, and can write a method that works with the type passed to
test it and see whether it is whatever the author wants it to be. The Find()
method uses the Predicate delegate method created to test each item in the
List, and returns the first item in the List for which the Predicate
delegate returns true.

As an example, let's say that we have a Generic List of type Int32:

private List<int> Numbers = new List<int>();

You want to find the first item in the List that is less than 5. So, you
create your method:

private static bool IsLessThanFive(int i)
{
return (i < 5);
}

So, now you call the Find() method:

int result = Numbers.Find(new Predicate<int>(IsLessThanFive);

The IsLessThanFive Predicate delegate is called for each item in the list,
passing that item as the parameter, until one of the calls returns true.
That is the number that is assigned to result.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
"Jeff S." <A@B.COM> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
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?

Thanks.


Mar 29 '06 #7
Thank you so much! (William and Kevin)...

-Jeff


"William Stacey [MVP]" <wi************@gmail.com> wrote in message news:Ow**************@TK2MSFTNGP09.phx.gbl...
Added another method to round out the example. Find3() takes a delegate we defined. This is how you might do the same thing before Predicate<T>.

public delegate bool PersonDel(Person p);

public class Customers

{

private List<Person> list;

public Customers()

{

list = new List<Person>();

}

public void Add(Person p)

{

if (p == null)

throw new ArgumentNullException("p");

lock (list)

{

list.Add(p);

}

}

// Prior to List<T>, you would do something like this. Can still do.

public Person Find(string name)

{

lock (list)

{

foreach (Person p in list)

{

if (p.Name == name)

return p;

}

return null;

}

}

// Passing delegate to List.Find() in 2.0.

public Person Find2(string name)

{

lock (list)

{

// Find will enumerate the list and eval the predicate for each item and return first item that returns true.

return list.Find(delegate(Person p)

{

return p.Name == name;

});

}

}

// Using your own delegate type to simulate what Predicate<T> does for you.

public Person Find3(PersonDel del)

{

lock (list)

{

foreach (Person p in list)

{

if ( del(p))

return p;

}

return null;

}

}

public static void Test()

{

Customers c = new Customers();

c.Add(new Person("Amy", 30));

c.Add(new Person("Joe", 35));

Person p = c.Find("Amy");

Console.WriteLine(p.ToString());

p = c.Find2("Joe");

Console.WriteLine(p.ToString());

p = c.Find3(delegate(Person person)

{

return person.Name == "Amy";

});

Console.WriteLine(p.ToString());

}

}

public class Person

{

public string Name;

public int Age;

public Person(string name, int age)

{

this.Name = name;

this.Age = age;

}

public override string ToString()

{

return Name + ", " + Age;

}

}
--
William Stacey [MVP]

Mar 29 '06 #8
Good point, Brian!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Kevin,

Excellent description. In addition you can use anonymous methods and
delegate inference to write it all in one line of code.

int result = Numbers.Find(delegate(int i) { return i < 5; });

Brian

Kevin Spencer wrote:
A delegate is like a function pointer. A Predicate delegate is a very
cool
use of Delegates which is used with various Generic Lists, Arrays and
Collections. Since a Generic List does not specify a data type, that is,
the
data type of the items in the List is set with a type parameter, how
would
one write a Find() function for a Generic List? Since nothing is known
about
the data type being searched for, one cannot know what sort of condition
is
used to discriminate between one item and another.

The Predicate delegate is a Generic method which takes an object of type
T
as a parameter. It returns true or false, indicating whether or not the
object of type T satisfies the condition it tests. The Predicate delegate
is
assigned the same type as the List when the List type parameter is passed
to
the instance of the Generic List created. The author of the code knows
what
the data type is, and can write a method that works with the type passed
to
test it and see whether it is whatever the author wants it to be. The
Find()
method uses the Predicate delegate method created to test each item in
the
List, and returns the first item in the List for which the Predicate
delegate returns true.

As an example, let's say that we have a Generic List of type Int32:

private List<int> Numbers = new List<int>();

You want to find the first item in the List that is less than 5. So, you
create your method:

private static bool IsLessThanFive(int i)
{
return (i < 5);
}

So, now you call the Find() method:

int result = Numbers.Find(new Predicate<int>(IsLessThanFive);

The IsLessThanFive Predicate delegate is called for each item in the
list,
passing that item as the parameter, until one of the calls returns true.
That is the number that is assigned to result.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
"Jeff S." <A@B.COM> wrote in message
news:um**************@TK2MSFTNGP12.phx.gbl...
>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?
>
> Thanks.
>

Mar 29 '06 #9

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

Similar topics

23
3544
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
3
10040
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
3
19454
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
6
2193
by: Stephen Johns | last post by:
I want to have a Hashtable whose keys are strings and whose values are delegates. Well, ok, I have that: dels = new Hastable(); dels.Add( "key", new Foo1Delegate(MyFoo1) ); dels.Add( "key",...
5
4221
by: han zhiyang | last post by:
Hi. I tried to design a custom web control which can flexibly and dynamicly let the control user ,for example the web page developer, customize its layout codes.This control derives from...
669
25378
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
13666
by: riceyeh | last post by:
Hi, What does <xsl:if test="not($values)"mean? What I do not understand is $values? Here, means array? And . = $value means current node is equal to the variable value? So the total meaning is...
2
2842
by: dkmd_nielsen | last post by:
I have two rather simple class methods coded in Ruby...my own each iterator: The iterator is used internally within the class/namespace, and be available externally. That way I can keep...
5
2383
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
0
7018
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
7183
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
7397
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...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4614
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3108
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
317
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.