SharpDeveloper
.NET Predicates (and a .RemoveAll() example)
A predicate is a function that returns true or false and can be used for example to filter your results.. For example your .NET RemoveAll(…) function can actually do things like:
Remove all records that say …
- "start with S"
- or "are 5 letters long"
- etc etc..
- whatever you wanna do.. GO WILD!
And knowing this, plus using our .NET 2.0 anonymous methods, we can do the above filtering in a single line! We don’t even need to write a special predicate function.
For example if you have a general list of ints, and you want to remove all ints that are equal to the value 5, you can do the following:
yourList.RemoveAll(new System.Predicate<int>( delegate(int val) { return (val == 5); }));
The keyword delegate(int val) { … } allows you to create an anonymous method.
Follow Up
Related Reading:
Other Interesting Posts
3 Responses to .NET Predicates (and a .RemoveAll() example)
Leave a Reply Cancel reply
-
Articles
- January 2011
- April 2010
- March 2010
- February 2010
- January 2010
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- February 2009
- December 2008
- November 2008
- October 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
-
Meta








kewl
awesome. saved me a lot of code. thanx
I’d really have to discuss with you here. Which isn’t something I usually do! I like reading a post that can make people think. Also, many thanks allowing me to comment!