Delegate to Lambda - Part 1
Well its been a while since I published my last post. I was very busy with my new job, visiting country and so on. Oh, by the way, I have been living in Finland for last 5 years and I am from Bangladesh. So, I have to go to visit my home country to see my family members. Anyway, today I will talk about delegate. Delegate is one of the most vital aspect of learning and understanding C#. When I first came across delegate, I sincerely didn't get a single clue about what's happening and why should I even need to use it? Later I came across LINQ which is full of Lambda expression and in reality it's mainly just Func(s). Well, (for now) Func is of type delegate that returns some value. Then there is Action and Predicate - other delegate types used in LINQ. Then there is event which is also a special type of delegate. So you see, understanding delegate is quite important as many other stuffs are directly linked to it. It's not good at all, if we jump into the ocean and try to learn all the stuff at a go rather we will start with the basics. So,

What is delegate?

Delegate is a function pointer. It is technically a class. Because when we declare a delegate, behind the scene compiler creates a class. The declaration of a delegate looks almost like a method declaration.

public delegate void MyDelegate();/*delegate that look likes a method declaration*/
//public class MyDelegate{ }/*compiler creates a class like this behind the scene*/

As behind the scene a class gets generated we can create instances of a delegate type, just like we do with any class type.

MyDelegate del_01 = new MyDelegate();
MyDelegate del_02 = new MyDelegate();

What is next?

At this point if we try to compile our code, we will get an error saying "does not contain a constructor that takes 0 arguments". Why it is so? Because, when we construct a delegate we have to pass down the 'Target', meaning a method-name that is compatible with the delegate signature. In our case MyDelegate is looking for a method that returns void and takes no arguments (signature of MyDelegate).
Now, there is something important to be considered. If the method, that we are going to pass, is an instance method then at first we have to create an instance of that object. If the method is not an instance method in other words static, then we can just simply pass it while creating a delegate instance. Lets see example of both sides.

Create compatible method

To pass method-name to MyDelegate let's create two methods those are compatible with the MyDelegate signature.

Method 1:

private void Hoo()
        {
            Console.WriteLine("Hoo()");
        }

Method 2:

private static void Foo()
        {
            Console.WriteLine("Foo();");
        }

Pass method name to delegate

Our first method is an instance method. That means we need to create a new instance of the class which the method originally belongs to. In our case all our method belong to class 'Program'. So, let's create a new instance of 'Program' and pass the instance method to MyDelegate constructor.

MyDelegate del_01 = new MyDelegate(p.Hoo);

Our second method is a static one. So, as I have mentioned earlier we can just pass the name of the method while creating a MyDelegate instance.

MyDelegate del_02 = new MyDelegate(Foo);

Invoke delegate

As you have noticed in both of our delegate reference we are not using "()" at the end of the method name. Well, you guessed it right! del_01 and del_02 are function pointers. They just point to one or many (more on this later) function(s). They don't invoke the function. Invocation happens some time later. 
In our case we do it the following way.

del_01();  
del_02();

Here is the whole picture for better understanding.

class Program
    {
        public delegate void MyDelegate();
 
        static void Main(string[] args)
        {
            var p = new Program();
            MyDelegate del_01 = new MyDelegate(p.Hoo);
            MyDelegate del_02 = new MyDelegate(Foo);
            del_01();
            del_02();
            Console.ReadLine();
        }
 
        private void Hoo()
        {
            Console.WriteLine("Hoo()");
        }
 
        private static void Foo()
        {
            Console.WriteLine("Foo();");
        }
 
    }

Continue : Part-2

May 10, 2014
Jahan Sarwar