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
How string definitions will prevent escaping on backslashes in C#?
What are value types in c#?
What is value c#?
What exactly is serverless?
How to implement singleton design pattern in c#?
Can the nested class access, the containing class. Give an example?
Can u list some style properties of List Box?
What is the difference between properties and indexer in c#?
What type is string in c#?
What is difference between ienumerable and iqueryable in c#?
What is the difference between delegates and events in c#?
What is mvc pattern in c#?
Explain About namespaces
What is iqueryable in linq?
What is literal in c#?