A Journey to Lambda
When i first hear this world lambda i was in my 6th or 7th
standard (well in reality i don't remember when so apprx). it was relatively a
Mathematics term. Then i get older (not
that much) then i start to learn Programming. I was learning C# and i hear it
again Lambda. Now how the hell lambda came back to Programming so i decided to
learn about it so here it is my journey to Lambda.
Part 1 : Delegates
It all start when Microsoft decide to put Delegates in C#.
Well some of you might remember pointer to function concept in your C++
classes. Microsoft decided to put the same functionality in the name of
delegates (Well actually delegates do much more then that).
1. here
we create a delegate
public delegate void del(string msg);
2. We
create methods with similar signature
public static void fun(string message)
{
Console.WriteLine(message);
}
3. and
assign that delegate's handler to the function
del handler = fun;
4. next
step is easy we just pass the required parameters to handler and it will
execute automatically
handler("Hello World");
In Totality Code if we create a Console Application 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
First when i learn about this Concept 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's look
like a stupid thing to me to do such kind of thing but actually i was wrong.
I can assign multiple function to the same delegate's
handler and execute them simultaneously. just like that.
5. So to
do this let's create another function.
public static void fun2(string abc)
{
Console.WriteLine("ABCD");
}
6. Assign this
as well to the handler
handler += fun2;
Note : be
Careful when i assign second function i had used += instead = operator.
now
execute the same 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)
{
//Some
file handling Code implementation here
//....
//...
//..
}
public static void WriteToDatabase(string abc)
{
//Some database
handling Code implementation here
//....
//...
//..
}
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 different-2 functions i can easily modify them and it will not affect the other code instead of creating a long method which can be difficult to maintain (you see i am handling difficult Database, File Handling, and Console Writing operations, just kidding!)
Our story will remain to be continued...
till then Au Revoir.
First welcome to Techfusion ,Mohit Sir (h) (h)
ReplyDelete,Nice artical on Lambda :>) :>)
Thanks Sir
Delete