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
Is learning c# hard?
You have got 1 million parking slots. At a time a parking slot can be free or not. To get next slot easily which data structure to implement?
What is the difference between icomparer and icomparable in c#?
What do you mean by the delegate in c#?
What are properties in c#. Explain with an example?
What are Types of assemblies that can be created in dotnet
What do you mean by synchronous and asynchronous operations?
What are floating point numbers?
When should I use static in C#?
What is out in c#?
What is dbml file in c#?
What is difference between const and static in c#?
Why would you use a class property in c#?
What is the use of static members with example using c#.net.
What are the 4 pillars of any object oriented programming language?