A Journey to Lambda : Part 2
So as of now we all know that it all started from the
Delegates. But Delegates was just the first step, Microsoft had taken. The Next
step was ....
Part 1 : Anonymous Methods
So what is anonymous Method, Well if i ask a student who had
not prepared his homework on anonymous methods he might say
Anonymous Methods are methods which does not have any name
And the strange thing is that, it is the right answer. They
don't have any name. As of last Article you know that i can assign any method
to the Delegate instance
del handler = fun;
and when execute the delegate by calling handler(). it will
execute the fun().
But let's suppose, if I say that I can execute the delegate
without assigning it to a method. So the fun function which was defined as
public static void fun(string message)
{
Console.WriteLine(message);
}
and assigned previously as
del handler
= fun;
are now replaced with this code
del handler
= delegate(string message)
{
Console.WriteLine(message);
};
and then if the code called in the same manner as
previously.
handler("Hello World");
code will produce similar output
So we can see that it is quite easy
to implement. as we don't need method name. Complete Solution code will look
like that
class Program
{
public delegate void del(string msg);
static void Main(string[] args)
{
del handler = delegate(string message)
{
Console.WriteLine(message);
};
handler("Hello World");
Console.ReadKey();
}
}
Part 2 : Func<T, TResult> and Action <T>
Delegates have special types as
well. Let's Dive into those special ones. In the above Scenario i had defined
the delegate and assign the method or anonymous method to it as
a) public delegate void del(string msg);
b) del handler = delegate(string message) {
Console.WriteLine(message);
};
or
del handler = fun;
where fun is the method name. So
clearly we have encapsulated the method (anonymous or defined) in the delegate
into our defined delegate. But you can use a Special Delegate which can encapsulate
the method without explicitly declaring your delegate so can totally avoid part a).
This Special Delegate is
Action<T> just like (T represent
for Type we can put any data type here, it represent what type of Parameter it
accept in this case it is string as <string> is written in those angular
braces.You can use your classes as well for T. Both Action <T> and
Func<T> are contravariant, So we can also use the types which
either are specified or are less derived.)
Action<string> handler = delegate(string message)
{
Console.WriteLine(message);
};
Well you can see that i have
included delegate declaration and instance creation simultaneously. and if we run the method we will see the same
output which was produced earlier.
Why Action<T>
if you have noticed the subtitle of the Section I had write down Func<T>
and Action <T> so why, well this
is a secret... just kidding, well if we look at our delegate signature of our
method or previous delegate we can easily say it was void type of delegate.
Mean it does not return any value. So when we don't return any value we use
Action<T> and when we return a value we use Func<T>
Now Let's see a Func<T>
Example.
Func<int, int, int> handler2 = delegate(int x, int y)
{
return x + y;
};
By checking Action<T> we can
easily say that in <int, int, int> there are three parameters which have
been passed. but if we see the delegate definition we only see two parameter.
The third one is the return type of the delegate as we have already told that
Func<T, TResult> return value on the other hand Action<T> don't
return any value. so we need a place
where we can define what is the type of the value returned and it has been
decided that it will be the last place of Func<T, TResult>. so in
nutshell last parameter is the return type. Now the complete code is as
following.
class Program
{
public delegate void del(string msg);
static void Main(string[] args)
{
del handler = delegate(string message)
{
Console.WriteLine(message);
};
Func<int, int,int> handler2 = delegate(int x, int y)
{
return x + y;
};
handler("Hello
World");
Console.WriteLine("5 + 10 = " +
handler2(5, 10));
Console.ReadKey();
}
}
and the Output is.
Well i hope you enjoy this session.
Our story will remain continue till the next part till
then au revoir.
(o)
ReplyDelete