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



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

Answer / 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

More C Sharp Interview Questions

Explain about c# language.

0 Answers  


What do you understand by 'access specifiers' in C#?

0 Answers   Genpact,


What do you mean by parsing and how to parse a date time string in c#?

0 Answers  


What is object pool in .net?

0 Answers  


What's the difference between abstraction and encapsulation?

0 Answers  






What is scavenging?

2 Answers   HCL,


What are the features of c#?

0 Answers  


what is the meaning of Object lifetime in OOPS

0 Answers   HCL,


How can I create a process that is running a supplied native executable (e.g., Cmd.exe)?

0 Answers  


Is a structure a class?

0 Answers  


What is difference between new and override in c#?

0 Answers  


What are the types of inheritance in c#?

0 Answers  


Categories