Monday, October 27, 2014

Anonymous Delegates

Anonymous delegates are the delegate where an anonymous method is embedded to its invocation list. Anonymous method do not have name, they have only code block called body. Three things involved in this process.

1. Delegate declaration

public delegate void mydelegate();


2. Create and object of this delegate and assign the anonymous method in it.

mydelegate del = delegate()
{
       Console.WriteLine("Anonymas delegate");
};


3. Invoke the method by the delegate.

del();


Output :
-start program-        
Anonymous delegate


Complete program is listed below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestDelegate
{
    public delegate void mydelegate();
    class program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-start program-");
            mydelegate del = delegate()
            {
                Console.WriteLine("Anonymas delegate");
            };
            del();
            Console.ReadKey();
        }
    }
}




Happy coding ... :)