Share via


Lambda, Lambdee, Lambduh: Methodically Anonymous

In the past blog: https://bit.ly/csharpdelegatessimplyMP900443191[1]

Now let’s get methodically anonymous, see, methods and anonymous, I really crack me up.

In our previous Lambda, Lambdee, Lambduh blog, I may have used code that I read from someone else's blog but can’t remember who, so please forgive me whoever it is, maybe leave a comment!

Ok, how is the anonymous expression used by delegates?

When the C# Compiler finds the delegate keyword inside of a method body, it expects that the delegate keyword will be followed by an anonymous body.  Memorize that.  Figure out why later.  It’ll come with experience.

In the following code I use the anonymous approach, but the way that I used the braces was to show that the anonymous methods are exactly that: Methods.

Code Snippet

  1. using System;
  2.  
  3. namespace SimplestDelegate
  4. {
  5.     /**************************************************
  6.      * Declare the delegate
  7.      *************************************************/
  8.     public delegate void DelegateSimply();
  9.  
  10.     class Program
  11.     {
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             /**************************************************
  16.              * Anonymous and delegate
  17.              *************************************************/
  18.             DelegateSimply delegateInstance = delegate()
  19.                 {
  20.                         string Ano = "Anonymous function first";
  21.                         Console.BackgroundColor = ConsoleColor.Cyan;
  22.                         Console.WriteLine(Ano);
  23.                 };
  24.             /**************************************************
  25.              * Use of the += attacher operator is shown
  26.              *************************************************/
  27.             delegateInstance += delegate()
  28.                 {
  29.                     string Ano =  "Anonymous function second";
  30.                     Console.BackgroundColor = ConsoleColor.Yellow;
  31.                     Console.WriteLine(Ano);
  32.                 };
  33.             /*********End of Anonymous and Delegate************/
  34.  
  35.             /*********Start of Delegate only*******************
  36.              * Delegate, not anonymous follows
  37.             /**************************************************
  38.              * Instantiate the delegate variable,
  39.              * pass the method
  40.              *************************************************/
  41.             DelegateSimply delSimply = new DelegateSimply(method1);
  42.             //Gingerbread decoration
  43.             Console.BackgroundColor = ConsoleColor.Blue;
  44.             /**************************************************
  45.              * Invocate the delegate
  46.              *************************************************/
  47.             delSimply();
  48.             /**************************************************
  49.              * Second instantiation for method 2
  50.              *************************************************/
  51.             delSimply = method2;
  52.             //Gingerbread decoration
  53.             Console.BackgroundColor = ConsoleColor.Red;   
  54.             /**************************************************
  55.              * Invocate the second instantation for method2
  56.              *************************************************/
  57.             delSimply();
  58.             /***********End of delegate***********************/
  59.             //Gingerbread decoration
  60.             Console.BackgroundColor = ConsoleColor.Black;
  61.             //
  62.         }
  63.  
  64.         public static void method1()
  65.         {            
  66.             Console.WriteLine("method1 was called by a delegate!");            
  67.         }
  68.  
  69.         public static void method2()
  70.         {                     
  71.             Console.WriteLine("method2 was called by a delegate!");            
  72.         }
  73.  
  74.     }
  75. }