By 22/07/2015 39 Comments

C# Anonymous Methods

  • C# methods that has not their name or in other words an unnamed block of codes which performs particular tasks are called Anonymous methods.
  • The anonymous methods are associated with a particular delegate type reference variable and it is created through the use of a keyword “delegate” with block of code.
  • Like named methods the anonymous methods can also have parameter-list.
  • The anonymous methods can return a value to the caller of the method (main method). These methods use “return” statement for retuning a value. That is a same “return” statement which is used with named return type methods.
  • The access modifiers can be used with anonymous methods.
  • In classes the anonymous methods can be declared as static or instance (non-static).

Example – 1, An anonymous void type method without parameters:

using System;
namespace csharpAdvance
{
    // void type delegate declaration.
    delegate void TableGenerate();

    class Tables
    {
        // An anonymous method declaration.
        public TableGenerate tableGenerate = delegate
        {
            int count = 1;
            for (int two = 2; two <= 20; two += 2, count++)
            {
                Console.WriteLine("2 * " + count + " = " + two);
            }
        }; // Notice (;) sign.
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Object creation of a Tables class.
            Tables tables = new Tables();

            // An anonymous method is called through an object reference.
            tables.tableGenerate();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        2 * 1 = 2
        2 * 2 = 4
        2 * 3 = 6
        2 * 4 = 8
        2 * 5 = 10
        2 * 6 = 12
        2 * 7 = 14
        2 * 8 = 16
        2 * 9 = 18
        2 * 10 = 20

     */
}


Example – 2, An anonymous void type method with 1 parameter:

using System;
namespace csharpAdvance
{
    // void type delegate declaration with 1 parameter.
    delegate void TableGenerate(int table);

    class Tables
    {
        // An anonymous method declaration with 1 parameter.
        public TableGenerate tableGenerate = delegate(int table)
        {
            int counting = 1;
            for (int count = table; count <= (table * 10); count += table, counting++)
            {
                Console.WriteLine(table+ " * " + counting + " = " + count);
            }
        }; // Notice (;) sign.
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Object creation of a Tables class.
            Tables tables = new Tables();

            // An anonymous method is called through an object reference.
            tables.tableGenerate(10);
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        10 * 1 = 10
        10 * 2 = 20
        10 * 3 = 30
        10 * 4 = 40
        10 * 5 = 50
        10 * 6 = 60
        10 * 7 = 70
        10 * 8 = 80
        10 * 9 = 90
        10 * 10 = 100

     */
}


Example – 3, A return type anonymous method with 1 parameter and static keyword:

using System;
namespace csharpAdvance
{
    // An int type parametrized delegate.
    delegate int Square(int digit);

    class Calculation
    {
        // A static return type anonymous method with 1 parameter.
        public static Square getSquare = delegate(int digit)
        {  
            return (digit * digit);

        }; // Notice (;) sign.
    }

    class Program
    {
        static void Main(string[] args)
        {
            // An static anonymous method getSquare(5) is being called through class reference.
            Console.WriteLine("Result is: {0}", Calculation.getSquare(5));
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Result is: 25

     */
}






Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .