What are anonymous methods ? why these methods are used and in what condition these methods are useful ?

Answer Posted / sudhir sheoran

Anonymous methods are used for making delegates
use simple. In this in spite of declaring a separate
method and then assigning a delegate to it we can
directly write inline code during the declaration
of delegate. E.g;

class Program
{
public delegate bool CalculatorDelegate(int number);
static void Main(string[] args)
{
CalculatorDelegate calDel = numberGreaterThanFive;
bool value = calDel.Invoke(4);
}
public static bool numberGreaterThanFive(int number)
{
if (number > 5)
{
return true
}
return false;
}
}

Now using Anonymous method and delegate :

class Program
{
public delegate bool CalculatorDelegate(int number);
static void Main(string[] args
{
CalculatorDelegate caldel =
new CalculatorDelegate(x => x > 5);

bool value = caldel.Invoke(3);
}

}

So code reduced to much extent. No need to define separate functions

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain about Error handling and how this is done

734


What is xml c#?

676


Howmany five tracing levels in System.Diagnostics.TraceSwitcher? Why they are using?

752


What is difference between array and list in c#?

639


Can we inherit abstract class in c#?

712


State two different types of access modifiers.

772


What is difference between a constant and read-only in C#?

736


What is an example of delegation?

693


Explain the Abstract class in c#.net

757


Do while loops yes or no c#?

730


What do you use c# for?

707


What is the task perform by clr?

766


Define multicast c# delegate?

743


What is the use of regex in c#?

692


What is cosole application?

780