Table of Contents
Anonymous Method in C # is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
We discussed that Delegate in C # helps 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. As an 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, As an example,, by passing method parameters to the Delegate object.
As an example,:
nc ( 10 );
As an 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 (); } } }
Frequently Asked Questions
What should you know about write an anonymous method in C #?
Anonymous methods (Anonymous Method) in C # are declared with the instance of the Delegate, with a delegate keyword. As an example,:
What is Anonymous Method in C #?
We discussed that Delegate in C # helps reference any method that has the same sign as in Delegate.
Why is Anonymous Method in C # important?
A clear understanding of Anonymous Method in C # helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.