Popular Posts

Oct 6, 2012

Introduce Anonymous Methods In C#




An anonymous method is, essentially, a block of code that is passed to delegate.


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

namespace AnoynmousMethos
{

    // Declare a delegate.   
    delegate void CountIt(); 

    class Program
    {
        static void Main(string[] args)
        {
            // Here, the code for counting is passed 
            // as an anonymous method.     
            CountIt count = delegate
            {
                // This is the block of code passed to the delegate.  
                for (int i = 0; i <= 5; i++)
                    Console.WriteLine(i);
            }; // notice the semicolon 

            count();

            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment