Jan 22, 2020

ANONYMOUS METHOD IN C#


anonymous method, c# anonymous delegate,c# anonymous event handler,inline delegate c#
anonymous method in c#

ANONYMOUS METHODS IN C# 

            “A method without any name. Its consists of only body of method called anonymous method.” For which we use delegate keyword to define a method, and comes in the delegate topic.
It is suggested when the volume of code is less with in few lines thus it better to create an unnamed  method and bind with delegate instead of making an original method. We never anonymous when the method having g large volume of code.
SYNTAX

     1.    Defining delegate

First we have to define a delegate of some name under namespace and should clear the return type at time of delegate defining let suppose we have a delegate name as Greeting_Delegate ( ) then under namespaces like this here I clear the return type also.
            Public delegate string Greeting_Delegate (string str);

     2.    Instantiating the delegate and anonymous binding the method

Now instead of defining and instantiating an instance and calling we direct define the method at the time of creating the delegate in main class of program let see at time of instantiating the instance
            Greeting_delegate Obj = delegate (string str)
           {
                ;       // the statement that has to execute under this delegate calling.
};
We write delegate keyword after that the parameter list and no need to specify the return type because already at the time of delegate defining we specified the return type. See the examples.

     3.    Calling the delegate

Same like other delegate it has to call like them first Obj (object name) and then pass the parameter but it can be done in two way. Consider the example below
1st way == Obj (“Asif”);                           //consider the upper delegate
2nd way == Obj. invoke (“Asif”);            // by using invoke method

Example of anonymous in c#

            We will clear the anonymous example by making two program of same logic in first program we will use the normal delegate to execute the program and after that we will use the anonymous to run the program.
1st Example
Delegate class
namespace AnoymMethod
{
    public delegate string GreetDelegate(string str);  //delegate defining stage 1
    class Greet
    {
        public static string Greeting(string str)
        {
            return "Hello " + str + " Welcome to Newmountain Hotel";
        }
    }
}

Main program
namespace AnoymMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            GreetDelegate Obj = new GreetDelegate(Greet.Greeting); //instance of delegate
            Console.WriteLine(Obj("Asif"));           //calling delegate stage 3
            Console.ReadKey();
        }
    }
}
2nd Examples the above program using anonymous method
Main program
namespace AnoymMethod
{
    public delegate string GreetDelegate(string str);  //defining delegate stage 1
    class Program
    {
        static void Main(string[] args)
        {
            GreetDelegate Obj = delegate (string str)   //anonymous method stage 2
            {
                return "Hello " + str + " Welcome to Newmountain Hotel";
            };
            Console.WriteLine(Obj("Asif"));  //calling delegate/anonymous method S-3
            Console.ReadKey();
        }
    }
}

 In above program under namespace we defined delegate by specifying the signature what will be the return type and how many parameter accept and which type that was stage 1.
 In main class instead of instantiating the instance we defined instance as well anonymous method by binding the delegate where we only specified the parameter list not the return type that was stage 2
 Finally we called the delegate who was responsible to execute the anonymous method that declare under it that was stage 3.
           

0 comments:

Post a Comment

Please do not enter any spam link in the comment box