anonymous method delegate invocation : Anonymous delegate « delegate « C# / CSharp Tutorial






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

    class Program
    {
        delegate void MessagePrintDelegate(string msg);

        static void Main(string[] args)
        {


            MessagePrintDelegate mpd2 = delegate(string msg)
            {
                Console.WriteLine("[Anonymous] {0}", msg);
            };
            LongRunningMethod(mpd2);

        }

        static void LongRunningMethod(MessagePrintDelegate mpd)
        {
            for (int i = 0; i < 99; i++)
            {
                if (i % 25 == 0)
                {
                    mpd(string.Format("Progress Made. {0}% complete.", i));
                }
            }
        }

        static void PrintMessage(string msg)
        {
            Console.WriteLine("[PrintMessage] {0}", msg);
        }
    }








9.4.Anonymous delegate
9.4.1.Create a delegate by declaration
9.4.2.Anonymous delegate method
9.4.3.An anonymous method.
9.4.4.An anonymous method that takes an argument.
9.4.5.An anonymous method that returns a value.
9.4.6.Demonstrate a captured variable.
9.4.7.Anonymous delegate methods
9.4.8.Anonymous Methods
9.4.9.anonymous method delegate invocation