Thursday, June 19, 2008

What is Delegate ?







What is Delegate ?




Delegates are a way to reference methods based on their signature and return type. It is also reffered as a type safe function pointers.Delegates are created at run time.It Used to call a method asynchronously.

Declaration:
               public delegate type_of_delegate delegate_name();


Example:


              public delegate void SampleDelegate(string message);

Sample Program :



Delegate with Named method


namespace DelegateSample

{

    public delegate void DisplayMessage(String sMessage);

                   

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

                   

        public void DispalyString(String sMessage)

        {

            MessageBox.Show(sMessage, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

                   

        private void btnDisplay_Click(object sender, EventArgs e)

        {

            try

            {

                DisplayMessage oDisplay = DispalyString;

                oDisplay("Sample Delegate...");

            }

            catch (Exception Ex)

            {

                throw (new Exception(Ex.Message));

            }

           

        }

    }

}

Delegate with Anonymous method



        private void btnDisplay_Click(object sender, EventArgs e)

        {

            try

            {

                // Delegate with Named method

                DisplayMessage oDisplay = DispalyString;

                oDisplay("Sample Delegate...");

             

                // Delegate with anonymous method

                DisplayMessage oDispAnonymous = delegate(String sMessage)

                {

                    MessageBox.Show(sMessage, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                };

                oDispAnonymous("Delegate With Anonymous Method...");

             

             

                oDisp("Delegate With Anonymous Method. Outside the function...");

             

            }

            catch (Exception Ex)

            {

                throw (new Exception(Ex.Message));

            }

           

        }

             

        // Delegate with anonymous method

             

        DisplayMessage oDisp = delegate(String sMessage)

        {

            MessageBox.Show(sMessage, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

        };

Multicast Delegate 

           It is a Delegate which holds the reference of more than one methods.Th multicast delegates return type should be "void" Otherwise it will throw the exception.

Sample Program :


namespace DelegateSample

{

    public delegate void DisplayMessage();

   

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

   

        public void DispalyString1()

        {

            MessageBox.Show("Execute First...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

   

        public void DispalyString2()

        {

            MessageBox.Show("Execute Second...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

   

        private void btnDisplay_Click(object sender, EventArgs e)

        {

            try

            {

                DisplayMessage oDisplay =new DisplayMessage(DispalyString1);

                // Add one more Method.

                oDisplay += new DisplayMessage(DispalyString2);

   

                oDisplay();

               

                // To Remove the First Method.

                oDisplay -= new DisplayMessage(DispalyString1);

   

                oDisplay();

   

            }

            catch (Exception Ex)

            {

                throw (new Exception(Ex.Message));

            }

         }

    }

}


conclusion 

            The delegate is one of the key concepts of C# and are very interesting aspects of the language.

 
 

No comments: