Lamda, Lamdee, Lamduh: Delegate first and then do the Lambda

imageSo you take your Java classes, you take your C++ classes and you take that data class, then you take that Operating System class that is cranking hard.  Now you are having fun with a great job and some boson shows the boss that it is much more efficient to write code using Lamda Expressions.  Where did that come from?  Lambda what the HECK!  Isn’t that some sort of FUNCTIONAL PROGRAMMING, and isn’t it used in F#, Scheme or OCAML, not C#.

Why is it called Anonymous?  Because you needed to learn how to spell anonymous correctly at some time in your life?  No.

An anonymous method is a method with no name.  Like one of those Clint Eastwood movies?  Well sort of.

To see accurate and concise articles about delegates, you must immeidately go to the MSDN discussion of Delegates: https://msdn.microsoft.com/en-us/library/ms173171(v=VS.80).aspx

Anonymous Expressions and delegates go together, so let’s take a look at an example of a delegate is (in this case you use a C# Console application).

If that is the case, that Anonymous expressions and delegates go together, let’s be sure we got the idea of delegates down, and then in the next blog I will cover the anonymous functions!

Code example of the use of a delegate (use a C# Console project) as we need to understand delegates somewhat to understand Lambda:

Code Snippet

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SimplestDelegate
  7. {
  8.     /**************************************************
  9.      * Declare the delegate
  10.      *************************************************/
  11.     public delegate void DelegateSimply();
  12.  
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             /**************************************************
  18.              * Instantiate the delegate variable,
  19.              * pass the method
  20.              *************************************************/
  21.             DelegateSimply delSimply = new DelegateSimply(method1);
  22.             //Gingerbread decoration
  23.             Console.BackgroundColor = ConsoleColor.Blue;
  24.             /**************************************************
  25.              * Invocate the delegate
  26.              *************************************************/
  27.             delSimply();
  28.             /**************************************************
  29.              * Second instantiation for method 2
  30.              *************************************************/
  31.             delSimply = method2;
  32.             //Gingerbread decoration
  33.             Console.BackgroundColor = ConsoleColor.Red;   
  34.             /**************************************************
  35.              * Invocate the second instantation for method2
  36.              *************************************************/
  37.             delSimply();
  38.             Console.BackgroundColor = ConsoleColor.Black;
  39.         }
  40.  
  41.         public static void method1()
  42.         {            
  43.             Console.WriteLine("method1 was called by a delegate!");            
  44.         }
  45.  
  46.         public static void method2()
  47.         {                     
  48.             Console.WriteLine("method2 was called by a delegate!");            
  49.         }
  50.  
  51.     }
  52. }

Code Snippet

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SimplestDelegate
  7. {
  8.     /**************************************************
  9.      * Declare the delegate
  10.      *************************************************/
  11.     public delegate void DelegateSimply();
  12.  
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             /**************************************************
  18.              * Instantiate the delegate variable,
  19.              * pass the method
  20.              *************************************************/
  21.             DelegateSimply delSimply = new DelegateSimply(method1);
  22.             //Gingerbread decoration
  23.             Console.BackgroundColor = ConsoleColor.Blue;
  24.             /**************************************************
  25.              * Invocate the delegate
  26.              *************************************************/
  27.             delSimply();
  28.             /**************************************************
  29.              * Second instantiation for method 2
  30.              *************************************************/
  31.             delSimply = method2;
  32.             //Gingerbread decoration
  33.             Console.BackgroundColor = ConsoleColor.Red;  
  34.             /**************************************************
  35.              * Invocate the second instantation for method2
  36.              *************************************************/
  37.             delSimply();
  38.             Console.BackgroundColor = ConsoleColor.Black;
  39.         }
  40.  
  41.         public static void method1()
  42.         {           
  43.             Console.WriteLine("method1 was called by a delegate!");           
  44.         }
  45.  
  46.         public static void method2()
  47.         {                    
  48.             Console.WriteLine("method2 was called by a delegate!");           
  49.         }
  50.  
  51.     }
  52. }

Now that we have seen what a delegate next, anonymous expressions or how I lambda the bomb.  Ok, maybe I need to work on the title.

(Note I use a slightly different type of comments, referred to as "flowers", don't do that in production, but I want to differentiate my comments from production types of comments)