Anonymous method instance resolution in C

  • 2020-10-23 21:11:30
  • OfStack

This article covers anonymous methods in C# in some detail, with examples to illustrate. Now I will share it with you for your reference. The specific analysis is as follows:

First, the anonymous methods in C# were introduced in C#2.0, ending the era when the only way to declare delegates in pre-ES6en #2.0 versions was to use named methods. Although C# 3.0 and later, Lambda expressions have replaced anonymous methods as the preferred way to write inline code. However, information about anonymous methods also applies to Lambda expressions, where Lambda expressions can be said to have evolved from anonymous methods.

We can use anonymous methods to ignore the argument list. This means that anonymous methods can be converted to delegates with various signatures, which is not possible with Lambda expressions. If you learn anonymous methods, you will have a deeper understanding of Lambda expressions.

Before we talk about the use of anonymous methods, let's talk about other terms for anonymous methods. Anonymous methods are also called anonymous delegates and anonymous functions, which are now common but somewhat different. msdn says that creating anonymous methods is the only way to pass a block of code as a delegate parameter. The anonymous method here is the official term, and because anonymous methods are used to pass blocks of code as delegate parameters, some people call them anonymous delegates, including the one I like, which I find easy to understand. As for anonymous functions, because of the concept of "methods" (Method) in C#, some languages call them "functions" (Function), so anonymous methods are also called anonymous functions. However, in the introduction to msdn, 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 still anonymous methods. Of course, other names are also widely circulated, so it is not necessary to be confused.

Here are some rules for writing anonymous methods:


delegate(int i) { return i > 0; }

It follows this format:


delegate( Parameter table ){ Method body code }

You can remember it in an analogy to js, where anonymous functions are written.

So where do anonymous methods work? How to use it? If you need a temporary method that is rarely used or if the code for the method is short, you can use anonymous methods. To give a simple example, if you need to select a new set in an integer set that meets the criteria, the following is the example


List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
// Suppose that you need to list Gets greater than in the set 3 All elements are returned as a new collection 
var newlist = newlist.FindAll(GetNewList);

GetNewList() is separately defined and delegated Predicate < T > Method with the same signature (Predicate < T > Is the built-in delegate of the system)

GetNewList() is defined as follows:


bool GetNewList(int i)
{
  return i > 3;
}

So that's how you would write it if you didn't use anonymous methods, and if you did, you would find that the 1 shear is so easy,


List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6 };
var newlist = list.FindAll(delegate(int i) { return i > 3; });

By comparison, the use of anonymous methods provides the same functionality as the previous named methods, but it no longer requires a method explicitly created before it is associated with the delegate, thus reducing the coding overhead required to instantiate the delegate, which is its biggest benefit.

I believe that this article has a certain reference value for your C# programming.


Related articles: