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).
- Here we create a delegate:
- public delegate void del(string msg);
- We create methods with a similar signature as in the following:
- public static void fun(string message)
- {
- Console.WriteLine(message);
- }
- And assign that delegate's handler to the function.
- The next step is easy, we just pass the required parameters to the handler and it will execute automatically as in the following:
In the totality of the code if we create a Console Application then it will looks like this:
- public delegate void del(string msg);
-
- public static void fun(string message)
- {
- Console.WriteLine(message);
- }
-
- static void Main(string[] args)
- {
- del handler = fun;
- handler("Hello World");
- 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.
- So to do this let's create another function.
- public static void fun2(string abc)
- {
- Console.WriteLine("ABCD");
- }
- Assign this as well to the handler:
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:
- public delegate void delWriter(string msg);
- public static void WriteToConsole(string message)
- {
- Console.WriteLine(message);
- }
- public static void WriteToFile(string abc)
- {
-
-
-
-
- }
- public static void WriteToDatabase(string abc)
- {
-
-
-
-
- }
- static void Main(string[] args)
- {
- del handler = WriteToConsole;
- handler += WriteToFile;
- handler += WriteToDatabase;
- handler("Hello World");
- 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.