Wednesday, April 18, 2012

Anonymous V/s Named method


delegate void Printer(string  s);
    class Program
    {
        static void Main(string[] args)
        {
            //Intialise delegate with Anonymous method.
            Printer _prn = delegate(string j)
            {
                System.Console.WriteLine(j);
            };
            //Calling anonymous method
            _prn("Hello anonymous method.");


            //Intialise delegate with Named method
            _prn = new Printer(Program.NamedMethod);
            //Calling named method.
            _prn("Hello named method");

            Console.ReadLine();
        }
        static void NamedMethod(string s)
        {
            System.Console.WriteLine(s);
        }
    }

No comments:

Post a Comment