Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

How can we sort an array in c#?

856


How can I develop an application that automatically updates itself from the web?

842


Describe the ways of cleaning up objects in c#.

851


What does question mark mean in c#?

937


Explain how to parse a datetime string?

873


Can we change static variable value in c#?

876


List the difference between the virtual method and the abstract method?

837


What is the difference between method and function in c#?

860


What is asynccallback c#?

958


Explain about ODP.net

1187


Is goto statement supported in c#?

884


What does the keyword “virtual” declare for a method or property?

905


How do we achieve encapsulation in c#?

894


Can we inherit static class in c#?

936


What is delegates in c# and uses of delegates?

791