This article describes the anonymous method in C# in more detail, and includes examples to illustrate it. Now I will share it with you for your reference. The specific analysis is as follows:
First, the anonymous method in C# was introduced in C# 2.0, which ended the era when the only way to declare delegates before C# 2.0 was to use named methods. Although in C# 3.0 and later, Lambda expressions replace anonymous methods as the preferred way to write inline code. However, the information of anonymous methods is also applicable to Lambda expressions. It can be said that Lambda expressions evolved from anonymous methods.
We can use anonymous methods to ignore the parameter list. This means that anonymous methods can be converted to delegates with various signatures, which is not possible with Lambda expressions. Only by learning the anonymous method can you have a deeper understanding of Lambda expressions.
Before talking about the use of anonymous methods, let’s talk about other names of anonymous methods. Some people call anonymous delegates and anonymous functions. Although they are generally common now, there is still a little difference. The official website of msdn says: To pass the code block as a delegate parameter, creating anonymous methods is the only way. The anonymous method here is the official statement, and because the anonymous method is to pass code blocks into delegate parameters, some people call it anonymous delegation, including the author also likes this name, I think it is easy to understand. As for anonymous functions, because some languages are called "function" in C#, anonymous methods are also called anonymous functions. However, in the msdn document introduction, anonymous functions include Lambda expressions and anonymous methods. It can be said that anonymous functions are at a higher level, so the most official name is anonymous method. Of course, other names are widely circulated, just understand them, there is no need to worry about them.
Next, let’s talk about the writing rules of anonymous methods, first the example:
delegate(int i) { return i > 0; }
It follows a format like this:
delegate(Formal parameters table){Method body code}
You can compare the writing of anonymous functions in js to remember.
So where are the anonymous methods used? How to use it? When you need a temporary method, which is used very few times or the code for this method you need is very short, then you can use anonymous method. Give a simple example, if you need to filter out a new set that meets the criteria in an integer set, as follows
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 }; //Suppose you need to get all elements greater than 3 from the list collection to return to the new collectionvar newlist = (GetNewList);
GetNewList() is a method defined separately and delegate with the same signature as Predicate<T> (Predicate<T> is a built-in delegate in the system)
GetNewList() is defined as follows:
bool GetNewList(int i) { return i > 3; }
The above is the writing method when there is no anonymous method. If you use anonymous method, you will find that everything has become so simple.
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 }; var newlist = (delegate(int i) { return i > 3; });
By comparison, it can be found that using anonymous methods can provide the same functionality as the previous naming methods, but it no longer requires a method that is explicitly created before being associated with the delegate, thus reducing the coding system overhead required to instantiate the delegate, which is its biggest benefit.
I believe that the description in this article has certain reference value for everyone's C# programming.