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

can multiple catch blocks be executed for a single try statement?

779


Is vs as c#?

723


Is it possible to nest cfml conditional tags?

758


What is an inheritance in c#?

693


How to use session under class file of APP_Code folder?

763


How you will create satellite assemblies?

718


What problem does Delegate Solve?

776


Explain the Abstract class in c#.net

782


What do you mean by string objects are immutable?

739


Which controls do not have events?

809


What is jagged array in c#?

782


What is a console file?

745


What is a virtual property. Give an example?

745


What is exe file in c#?

693


In howmany ways can you deploy an assembly?

744