Friday, May 02, 2008

Basic C# Generic Predicate Delegate

Find a very useful (practical) example of combining generic, predicate, and delegate together on the List (removing items in the list).

//create a list of candidates
List candidates = new List();
for (int i=2; i <= 100; i++) { candidates.Add(i); } //use predicate and delegate to remove the items on the list candidates.RemoveAll (delegate(int x) { return x>2&& x%2==0; }

//or use the predicate only

static bool RemoveUnWantedItems(int x)
return (x>2&& x%2==0;);

No comments: