Topic : Anonymous Method in C# Programming Language:

BASIC INSTRUCTION BEFORE GETTING START:

An Anonymous Method is a method without a Name, that's why we called it Anonymous. This method let us declare a method body without giving it a name. actually this method exist as normal method but there is no way to explicitly call anonymous method in our code, but we can use these method using Delegates


Delegates:

We already discussed about Delegates in our Previous tutorial. Delegate are reference type data type and it holds the reference of a Method. We can call a Method that can be referenced by a delegate using the delegate object.


Anonymous Methods provide a technique to pass a code block as a delegate Parameter. Using Anonymous methods, we can pass block of code rather that the name of the method.



Declaring Anonymous Method:

We can define Anonymous methods in C# using the Delegate keyword and can be assigned to a variable of delegate type as shown in this example:


/*Example -Anonymous Method - InfoBrother*/

using System;

namespace Anonymous
{
    class Program
    {
        public delegate void message(int ID);  //delegate Declare: 

        static void Main(string[] args)
        {
            message msg = delegate (int id)
            {
                //Anonymous Method Body:
                Console.WriteLine("Hi this is Anonymous, the ID is: " + id);
            };

            msg(141);  //Named Method Calling:
           

            Console.ReadKey();

        }
    }
}


The Delegate could be called both with anonymous methods as well as named methods in the same way, i.e by passing the method parameters to the delegate object.



  • Anonymous Method Limitations:

    » It cannot contain jump statement like goto, break or continue.
    » It cannot access ref or out parameter of an outer method.
    » It cannot have or access unsafe code.
    » It cannot have or access unsafe code.



Anonymous Method as Parameter:

Anonymous methods can also be passed to a method that accepts the delegate as a parameter. Let's have an example to know how its work.



/*Example -Anonymous Method as Parameter - InfoBrother*/

using System;

namespace Anonymous
{
    public delegate void message(int Id);  //delegate Declare: 

    class Program
    {
        public static void displayMsg(message text, int ID)
        {
            ID += 24;
            text(ID);
        }

        static void Main(string[] args)
        {

            displayMsg(delegate (int ID)
           {
               //Anonymous Method Body:
               Console.WriteLine("Hi this is Anonymous and the ID is: " + ID);
           }, 101);

            Console.ReadKey();

        }
    }
}


  • More about Anonymous Method:

    » Anonymous method can be defined using the delegate keyword
    » Anonymous method must be assigned to a delegate.
    » Anonymous method can access outer variables or functions.
    » Anonymous method can be passed as a parameter.
    » Anonymous method can be used as event handlers.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement