|
Technologies |
|
|
 |
|
|
|
|
|
|
Predicate Delegate |
|
|
|
|
Predicate Delegate
A delegate is like a function pointer.
A Predicate delegate is a very cool use of Delegates which is used with 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 Numbers = new List();
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(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.
In .NET 2.0 it is possible to declare
"anonymous method", so we can avoid declaring separate method
whatsoever:
int
result = Numbers.Find(delegate(int x) { return x < 5; });
New .NET 3.5 syntax is even simpler, we can completely avoid
"new Predicate" statement (it will be in fact created by compiler in
background):
int
result = Numbers.Find(IsLessThanFive);
|
|
|
Additionally, .NET 3.5 introduces "lambda expressions", so finally our array
search becomes incredibly simple:
int result = Numbers.Find(x => x <
5);
|
Similar Articles
You can contribute to CodeDigest.Com:
|
| Article Feedback |
|
|
|
Comments
|
ZEFEbB , [url=http://ifjdnljsevug.com/]ifjdnljsevug[/url], [link=http://tvrkkwmusrqi.com/]tvrkkwmusrqi[/link], http://toebrqkyqrpr.com/
ZEFEbB , [url=http://ifjdnljsevug.com/]ifjdnljsevug[/url], [link=http://tvrkkwmusrqi.com/]tvrkkwmusrqi[/link], http://toebrqkyqrpr.com/
Commented By ZEFEbB , [url=http://ifjdnljsevug.com/]ifjdnljsevug[/url], [link=http://tvrkkwmusrqi.com/]tvrkkwmusr
on 7/8/2011 @ 7:49 AM
|
JtUVZF , [url=http://vggxmwuqoomz.com/]vggxmwuqoomz[/url], [link=http://gdwjotyiuoje.com/]gdwjotyiuoje[/link], http://dfaqurhtlndx.com/
JtUVZF , [url=http://vggxmwuqoomz.com/]vggxmwuqoomz[/url], [link=http://gdwjotyiuoje.com/]gdwjotyiuoje[/link], http://dfaqurhtlndx.com/
Commented By JtUVZF , [url=http://vggxmwuqoomz.com/]vggxmwuqoomz[/url], [link=http://gdwjotyiuoje.com/]gdwjotyiuo
on 7/5/2011 @ 4:48 AM
|
mD0BoJ <a href="http://fyyxwknfovdq.com/">fyyxwknfovdq</a>
mD0BoJ <a href="http://fyyxwknfovdq.com/">fyyxwknfovdq</a>
Commented By mD0BoJ <a href="http://fyyxwknfovdq.com/">fyyxwknfovdq</a>
on 7/4/2011 @ 2:18 AM
|
One or two to remmeebr, that is.
One or two to remmeebr, that is.
Commented By One or two to remmeebr, that is.
on 7/3/2011 @ 9:41 PM
|
Pirated example Plagerism
http://bytes.com/topic/c-sharp/answers/474304-what-predicate-delegate
Please give credit where credit is due
Commented By MKK
on 5/26/2010 @ 6:25 PM
|
Pirated example Plagerism
Please give credit where credit is due
Commented By MKK
on 5/26/2010 @ 6:23 PM
|
|
|
|
|
|
|
|
|
|
|
|