So as of now we all know, it all started with the delegates. But delegates were just the first step Microsoft took. The next step was anonymous methods.
Part 1 : Anonymous Methods
So what is an anonymous method? Well if I ask a student that has not prepared his homework on anonymous methods he might say:
"Anonymous Methods are methods that do not have a name."
And the strange thing is that, it is the right answer. They don't have a name. As of the last article you know that I can assign any method to the delegate instance.
del handler = fun;
And when the delegate is executed by calling handler(), it will execute fun().
But suppose I can execute the delegate without assigning it to a method. So the fun function that was defined as:
- public static void fun(string message)
- {
- Console.WriteLine(message);
- }
- del handler = fun;
- del handler = delegate(string message)
- {
- Console.WriteLine(message);
- };
- handler("Hello World")
Output

- class Program
- {
- public delegate void del(string msg);
- tatic void Main(string[] args)
- {
- del handler = delegate(string message)
- {
- Console.WriteLine(message);
- };
- handler("Hello World");
- Console.ReadKey();
- }
- }
Delegates have special types as well. Let's dive into those special ones. In the above scenario I defined the delegate and assign the method or anonymous method to it as:
- public delegate void del(string msg);
- del handler = delegate(string message) {
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 that can encapsulate the method without explicitly declaring your delegate so you can totally avoid a declaration section as desribed as 1.
This Special Delegate is Action<T> just like (T represents Type we can use any data type here, it represents what type of Parameter it accept in this case it is string as <string> and is written in those angular braces. You can use your classes as well for T. T for Both Action <T> and Func<T, TResult> are contravariant (but TResult is Covariant), so we can also use the types that either are specified or are less derived as parameters.)
- Action<string> handler = delegate(string message)
- {
- Console.WriteLine(message);
- };
Well you can see that I have included a delegate declaration and instance creation simultaneously and if we run the method we will see the same output that was produced earlier.
Why Action<T>? Notice the subtitle of the Section. I had written 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 the previous delegate we can easily say it was a void type of delegate. In oher words 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;
- };
- 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();
- }
- }

Our story will be continued in the next part. Until then au revoir.

vijayakumarPosted Feb 23, 2015, 2:07 AM
Thank you it is nice one
Mohit SharmaPosted Jul 30, 2014, 2:57 AM
Thank you very much sir, this series is dedicated to you.
Chris EarglePosted Jul 29, 2014, 9:27 AM
Cool stuff. I remember trying to wrap my head around anonymous methods back in 2006. Crazy times!