Anonymous method in C #

We discussed that Delegate in C # is used to reference any method that has the same sign as in Delegate. In other words, you can call a method that can be referenced by a Delegate using that Delegate object.

An anonymous method (Anonymous Method) in C # provides a technique to pass a code block as a delegate parameter. Anonymous methods are methods without names, only method bodies.

You do not need to specify the return type in an anonymous method; it is derived from the return statement inside that anonymous method body.

Write an anonymous method in C #

Anonymous methods (Anonymous Method) in C # are declared with the instance of the Delegate , with a delegate keyword. For example:

 delegate void NumberChanger ( int n ); . NumberChanger nc = delegate ( int x ) { Console . WriteLine ( "Anonymous Method: {0}" , x ); }; 

Console.WriteLine ("Anonymous Method: {0}" block , x); is the body of the anonymous method.

Delegate can be called both with anonymous methods as well as named methods in the same way, for example, by passing method parameters to the Delegate object.

For example:

 nc ( 10 ); 

For example:

Here is an example illustrating the concept above:

 using System ; delegate void NumberChanger ( int n ); namespace Vd Delegate { class QTMDelegate { static int num = 20 ; public static void AddNum ( int p ) { num += p ; Console . WriteLine ( "Phương thức được đặt tên: {0}" , num ); } public static void MultNum ( int q ) { num *= q ; Console . WriteLine ( "Phương thức được đặt tên: {0}" , num ); } public static int getNum () { return num ; } static void Main ( string [] args ) { //Tạo delegate sử dụng phương thức nặc danh NumberChanger nc = delegate ( int x ) { Console . WriteLine ( "Phương thức nặc danh: {0}" , x ); }; //Gọi delegate sử dụng phương thức nặc danh nc ( 11 ); //Khởi tạo delegate sử dụng phương thức được đặt tên nc = new NumberChanger ( AddNum ); //Gọi delegate sử dụng phương thức được đặt tên nc ( 10 ); //Khởi tạo delegate sử dụng phương thức được đặt tên khác nc = new NumberChanger ( MultNum ); //Gọi delegate sử dụng phương thức được đặt tên nc ( 5 ); Console . ReadKey (); } } } 

When you run the C # program, you will get the results on the screen as follows:

 Anonymous method: 11 
Named method: 30
Named method: 150

According to Tutorialspoint

Previous article: Generic in C #

Next article: Unsafe code in C #

4 ★ | 1 Vote

May be interested

  • Veil: anonymous browser deletes all traces, safer than TorVeil: anonymous browser deletes all traces, safer than Tor
    those who want to delete their browsing history without leaving any trace, which most anonymous browsers are currently unable to do, veil will be the perfect choice for that.
  • New generation extortion trojan detectionNew generation extortion trojan detection
    the new generation of extortion trojans is much more dangerous because of the use of an anonymous tor network and a stronger encryption method.
  • Anonymous browsing: can protect your privacyAnonymous browsing: can protect your privacy
    perhaps you are no stranger to anonymous surfing, or private browsing - a mode that when you use it to surf the web, the browser will not save any of your bookmarks. in addition to the somewhat ... shady goals, this regime also serves other legitimate reasons.
  • How to use Mailspre to send anonymous emailsHow to use Mailspre to send anonymous emails
    mailspre is an unlimited email service, without account registration.
  • How to Send Anonymous MessagesHow to Send Anonymous Messages
    communicating in today's increasingly virtual community has two sides: interesting and easily boring. although most people prefer anonymity, it is not easy to maintain it. there are many different ways to text anonymously. let's read the article below to discover how to send anonymous messages.
  • Getting 5 'Anonymous' suspects in DDoS attackGetting 5 'Anonymous' suspects in DDoS attack
    five suspects men are said to be 'anonymous' in ddos attacks on paypal, visa, mastercard and amazon website systems have just been arrested. this anonymous group has succeeded in disrupting services for the above-mentioned websites, except for amazon.com.
  • Facebook application development allows anonymous users?Facebook application development allows anonymous users?
    more information indicates that facebook is building a secret application, allowing users to interact and comment 'anonymously'.
  • How to delete browsing traces on MacHow to delete browsing traces on Mac
    when you surf the web on someone else's mac, you will accidentally leave a web browser. and surely we will want those browsing history to be erased on the mac when in use,
  • Anonymous on Facebook, hide activities on Facebook, completely invisibleAnonymous on Facebook, hide activities on Facebook, completely invisible
    you want to remain anonymous on facebook, don't tell your friends when you're online, or in other words how to completely stealth on facebook? so you have found the right place!
  • Anonymous declared cyberwar with SyriaAnonymous declared cyberwar with Syria
    the media said, the largest hacker group in the world anonymous has announced it will conduct cyberwar against the syrian government, just a day after the country caused outrage by blocking internet access of people. .