Proxy is a special method that points to the address of a method module. Generally, the method module can be a common method. More often, it is an anonymous Lambda expression, that is, an anonymous method. Now let's take a brief look at the abbreviated proxy method, that is, the Action keyword.
class A { B b = new B(); public delegate string Show(string result); public string Execute() { Show s = new Show(b.MyShow); string str = s.Invoke("ttt"); return str; } } class B { public string MyShow(string s) { return s + ">>>>>>>>>"; } } static void Main(string[] args) { A a = new A(); a.Execute(); }In this way, when A is used, only the MyShow code in B is changed, and the execution result of Execute in A can be customized. Code with the same function is completed using the Action type.
class C { D d = new D(); Action
action; public void Execute() { action = d.MyShow2; action.Invoke("ttt"); } } class D { public void MyShow2(string s) { Console.WriteLine(s + ">>>>>>>>>"); } } static void Main(string[] args) { A a = new A(); a.Execute(); }
This code has the same effect as the above Code. It can be seen that, in essence, Action is the simplified mode of delegate, just like lamda expressions to simplify anonymous methods. When an anonymous method must have a return value, we use the Fun type for processing. Basically, actions are the same in usage. For parameters, the first few are input values, and the last one is the return value.