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 #
You should read it
- Who is Anonymous?
- Anonymous browsing has more uses than you think
- 5 methods of emailing are completely anonymous
- Send anonymous anonymous emails with these 18 great websites
- Call the function by reference in C ++
- Learn about the Write Zero method
- The best free anonymous proxy servers
- Among Us is about to have anonymous votes?
May be interested
- Safe code in C #c # allows using pointer variables in a function of code block when it is marked by unsafe modifier. the concept of unsafe code or unmanaged code in c # is a code block that uses a pointer variable.
- Multithread (Multithread) in C #[thread in c #] a thread is defined as an execution path (execution path) of a program. each thread defines a single control line. if your application includes complex and time-consuming activities, it is often useful to set up execution paths or thread, with each thread performing a specific task.
- Property (Property) in C #properties - property is the member named for the layer, structure, and interface. member variables or methods in a class or structure are called fields. attribute is an inheritance of fields and is accessed using the same syntax. they use accessor through the values of private fields that can be read, written, and manipulated.
- What is C #c # is a simple, modern, general-purpose, object-oriented programming language developed by microsoft and approved by the european computer manufacturers association (ecma) and international standards organization (iso).
- Installing C # environmentin this chapter, we will introduce the tools needed to install the c # environment. we mentioned that c # is part of the .net framework and is used to write .net applications. therefore, before discussing the tools available to run a c # program, you should understand how c # relates to the .net framework.
- C # program structurebefore learning how to build c # programming language blocks, we should explore the basic c # program structure.