What are anonymous methods ? why these methods are used and in what condition these methods are useful ?
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 |
What are methods in C#?
What is function c#?
What is the use of New Keyword
What is the meaning of console writeline in c#?
What is generic delegates in c#?
Can we write one page in c# and other in vb in one application ?
Is int a struct in c#?
What is difference between class and interface in c#?
What does async mean in c#?
What framework is used for performance testing/load testing?
What is c-sharp (c#)?
Can a sealed class be used as a base class?