first_page

The Predicate Delegate

Buy this book at Amazon.com!In “Jean-Paul Boodhoo on Demystifying Design Patterns Part 4,” I’m almost sure the concept of the Predicate Generic Delegate was introduced to me. This led to a post called “The Predicate Delegate” by Jeremy Jarrell. But what has been subtle to me is that the syntax for this Predicate is different for the static Array search methods and generic lists. So the ‘older’ Array stuff looks like this:public static void Main() {

Point[] points = { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) };

Point first = Array.Find(points, ProductGT10); } private static bool ProductGT10(Point p) { return (p.X * p.Y > 100000); } And the ‘newer’ generic list stuff looks like this:public static void Main() { List<Point> points = new List<Point>();

Point[] temp = { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) };

foreach ( Point p in temp) { points.Add(p); }

Point first = points.Find(new Predicate<Point>(ProductGT10)); }

private static bool ProductGT10(Point p) { return (p.X * p.Y > 100000); } Since Jean-Paul is a typical young cat, he frequently is only concerned with the new stuff. And my use of that temp Array to populate the generic list points in the example above smells like old socks. When the C# 3.0 comes out, its “object initializers” will be available. And the code will look like this:public static void Main() {

List<Point> points = new List<Point> { new Point(100, 200), new Point(150, 250), new Point(250, 375), new Point(275, 395), new Point(295, 450) };

Point first = points.Find(new Predicate<Point>(ProductGT10)); }

private static bool ProductGT10(Point p) { return (p.X * p.Y > 100000); } Microsoft’s Mike Taulty videos this new technology in “C# Version 3.0—Object Initialisers” and “VB9—Object Initialisers.”

rasx()