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

What is asynccallback c#?

0 Answers  


What is the correct way of declaring an xml namespace?

0 Answers  


What is mvc in c#?

0 Answers  


What is a delegate? Explain.

0 Answers  


What is the difference between private and protected in c#?

0 Answers  


What are the benefits of using windows services:

0 Answers  


Why abstraction is used in c#?

0 Answers  


What is difference between event and delegate in c#?

0 Answers  


Can you declare a class or a struct as constant?

0 Answers  


Okay, so an int is a value type, and a class is a reference type. How can int be derived from object?

0 Answers  


What are methods in C#?

0 Answers   Winsol Solutions,


Is c# an open source language?

0 Answers  


Categories