A Journey to Lambda

When I first heard this word lambda I was in my sixth or seventh standard (well in reality I don't remember when so that is approximate). It was a relative Mathematics term. Then I got older (not that much) then I began to learn programming. I was learning C# and I heard lambda again. Now how the hell is lambda relevant to programming? I decided to learn about it so here is my journey to lambda.

Part 1: Delegates

It all starts when Microsoft decided to put delegates in C#. Well some of you might remember function pointers in C++ classes. Microsoft decided to use the same functionality as delegates (well actually delegates do much more then that).

  1. Here we create a delegate:
    1. public delegate void del(string msg); 
  2. We create methods with a similar signature as in the following:
    1. public static void fun(string message)  
    2. {  
    3.    Console.WriteLine(message);  

  3. And assign that delegate's handler to the function.
    1. del handler = fun; 
  4. The next step is easy, we just pass the required parameters to the handler and it will execute automatically as in the following:
    1. handler("Hello World"); 
    In the totality of the code if we create a Console Application then it will looks like this:
    1. public delegate void del(string msg);  
    2.   
    3. public static void fun(string message)  
    4. {  
    5.    Console.WriteLine(message);  
    6. }  
    7.   
    8. static void Main(string[] args)  
    9. {  
    10.    del handler = fun;  
    11.    handler("Hello World");  
    12.    Console.ReadKey();  

    Here is the output:

    output

    When I first learned about this I thought what the hell is that. Why would I assign my function to a delegate's handler and then use that to execute it. I mean I can directly execute my own function, it looks like a stupid thing to me to do such kind of thing but actually I was wrong.

    I can assign multiple functions to the same delegate's handler and execute them simultaneously, just like that.
     
  5. So to do this let's create another function.
    1. public static void fun2(string abc)  
    2. {  
    3.    Console.WriteLine("ABCD");  
    4. }
  6. Assign this as well to the handler:
    1. handler += fun2; 
    Note: be careful, to assign a second function use += instead of the = operator.

    Now execute the same code.

    code

    Some of you still might be thinking Ok we can call multiple methods with the single call, but what is the benefit? See the following code:
    1. public delegate void delWriter(string msg);  
    2. public static void WriteToConsole(string message)  
    3. {  
    4.    Console.WriteLine(message);  
    5. }  
    6. public static void WriteToFile(string abc)  
    7. {  
    8.      //Some file handling Code implementation here  
    9.      //....  
    10.     //...  
    11.     //..  
    12. }  
    13. public static void WriteToDatabase(string abc)  
    14. {  
    15.      //Some database handling Code implementation here  
    16.      //....  
    17.     //...  
    18.    //..  
    19. }  
    20. static void Main(string[] args)  
    21. {  
    22.     del handler = WriteToConsole;  
    23.     handler += WriteToFile;  
    24.     handler += WriteToDatabase;  
    25.     handler("Hello World");  
    26.     Console.ReadKey();  

Here the implementation says that I can write to the screen as well as on the file and the database at the same time.

Plus I get one more benefit since all the code is in 2 functions, I can easily modify them and it will not affect the other code instead of creating a long method that can be difficult to maintain (you see I am handling a difficult database, doing file handling and console writing operations, just kidding!)

Our story will continue. Until the next part, au revoir.

Up Next
    Ebook Download
    View all
    Learn
    View all