Tuesday, August 21, 2007

Anonymous delegates are a very neat and useful feature in C# 2.0. The idea of keeping your code tight and simple without having to move all over your code to understand what it is supposed to be doing is always a welcomed feature in today's complex coding endeavors.

Let's take for example a simple class of Mediterranean Food below that just declares a name and description properties:

 
1 public class MediterraneanFood 2 { 3 private string _name; 4 public string Name 5 { 6 get 7 { 8 return _name; 9 } 10 } 11 12 public string Description 13 { 14 get 15 { 16 return _description; 17 } 18 } 19 20 private string _description; 21 22 23 public MediterraneanFood(string name, string description) 24 { 25 _name = name; 26 _description = description; 27 } 28 }

The goal of the code I am going to be writing below is to create a list of Mediterranean foods and then search the list for a specific food.  As you will see the code below, I tried to show how it is done in 3 different ways (Very old, old and Anonymous Delegates way) just to give you an idea of what the difference is and the code changes needed to bring the code to the 21st century :-)

In the first way (Very Old) we just go through the list using foreach to find the specific food we are looking for.  What's the fun in that! :-)

The second way (Old) I declared a delegate called "OldFindFood" and then in the code we pointed to that delegate via a predicate (type of a delegate) and let the delegate do the search for us.  Problem here of course is the meat of what it is supposed to do is far away from where it is being used in the code.

Finally, I used an Anonymous Delegate which does not need any of the previous code.  The anonymous delegate is declared on the fly and implemented right after the declaration making easy and simple to understand what the intent of this code supposed to be doing.

Have fun, let me know if you have questions or if you have a different opinion.

1 private static bool OldFindFood(MediterraneanFood foodToFind) 2 { 3 return ("Kabob" == foodToFind.Name); 4 } 5 6 private static void ShowFood(MediterraneanFood food) 7 { 8 Console.WriteLine("Name: {0} Description: {1}", 9 food.Name, food.Description); 10 } 11 12 static void Main(string[] args) 13 { 14 15 List<MediterraneanFood> mediterraneanFoods = new List<MediterraneanFood>(); 16 mediterraneanFoods.Add(new MediterraneanFood("Falafel", "Made to Order!!")); 17 mediterraneanFoods.Add(new MediterraneanFood("Kabob", "Meat on a skewer")); 18 mediterraneanFoods.Add(new MediterraneanFood("Babaganoush", "Eggplant dip")); 19 mediterraneanFoods.Add(new MediterraneanFood("Dolmas", 20 "Stuffed grape leaves, with meat")); 21 22 23 MediterraneanFood foundFood; 24 25 // -- Very Old -- 26 Console.WriteLine("\nVery old:"); 27 foreach (MediterraneanFood food in mediterraneanFoods) 28 { 29 if (food.Name == "Kabob") 30 { 31 ShowFood(food); 32 } 33 } 34 35 // -- Old -- 36 Console.WriteLine("\nOld:"); 37 38 System.Predicate<MediterraneanFood> myPredicate = 39 new Predicate<MediterraneanFood>(OldFindFood); 40 foundFood = mediterraneanFoods.Find(myPredicate); 41 ShowFood(foundFood); 42 43 // -- Anonymous Delegate -- 44 Console.WriteLine("\nAnonymous Delegate:"); 45 List<MediterraneanFood> foods = 46 mediterraneanFoods.FindAll(delegate(MediterraneanFood foodToFind) 47 { return (foodToFind.Description.ToLower().Contains("meat")); }); 48 foreach (MediterraneanFood food in foods) 49 { 50 Console.WriteLine(food.Description); 51 } 52 53 Console.ReadLine(); 54 } 55 }
Tuesday, August 21, 2007 10:59:15 AM (Pacific Standard Time, UTC-08:00)  #    Comments [2]   ASP.NET | C# | Falafel | Technology  | 
Tuesday, August 28, 2007 1:31:25 PM (Pacific Standard Time, UTC-08:00)
On line 46, I seem to rememember from your presentation at TechEd in Orlando, that there was some issue related to the name of the parameter? Let's see...was it findToFood? foundToFound? Can't quite remember... ;-)
John Waters
Tuesday, August 28, 2007 1:33:34 PM (Pacific Standard Time, UTC-08:00)
Nope, that is not correct!

It was on line 47
:)
Comments are closed.