About C# anonymous methods

A C# anonymous method is an "inline" statement or expression that can be used anywhere a delegate type is expected. You can use an anonymous function to initialize a named delegate, or pass a named delegate as a method parameter. The following describes C# anonymous methods.

1. Declaration of C# anonymous method

Anonymous functions can be created using lambda expressions or anonymous methods. Lambda expressions are recommended because they provide a more concise and expressive way to write inline code. Unlike anonymous methods, certain types of lambda expressions can be converted to expression tree types. The anonymous method definition syntax is as follows,

1) Use the delegate keyword to create a delegate instance to declare

delegate void MyDelegate(string s);
...
MyDelegate m = delegate(string x)
{
    Console.WriteLine("匿名方法: {0}", x);
};

 2) Use lambda expressions

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace cjavapy
{
    delegate void MyDelegate(string s);
    class Program
    {
        static void Main(string[] args)
        {
            //Expression<del> myET = x => x * x;
            MyDelegate m =  x=>Console.WriteLine("匿名方法: {0}", x);
            Console.ReadKey();
        }
    }
}

 

2. The use of anonymous methods

In C# 1.0, an instance of a delegate is created by explicitly initializing the delegate using a method defined elsewhere in the code. C# 2.0 introduced the concept of anonymous methods as a way to write unnamed inline blocks of statements that can be executed within a delegate call. C# 3.0 introduced lambda expressions, which are similar to the concept of anonymous methods, but more expressive and more concise. These two functions are collectively referred to as anonymous functions. In general, applications targeting .NET Framework 3.5 or later should use lambda expressions.

For example,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace cjavapy
{
    class Test
    {
        delegate void MyDelegate(string s);
        static void MyMethod(string s)
        {
            Console.WriteLine(s);
        }
        static void Main(string[] args)
        {
            //原始委托语法
            //使用指定方法初始化。
            MyDelegate md1 = new MyDelegate(MyMethod);
            // c# 2.0:一个委托可以被初始化
            //内部代码,称为“匿名方法”。这
            //方法以字符串作为输入参数。
            MyDelegate md2 = delegate (string s) { Console.WriteLine(s); };
            // c# 3.0。委托可以被初始化
            //一个lambda表达式lambda也接受一个字符串
            //作为输入参数(x)。x的类型由编译器推断。
            MyDelegate md3 = (x) => { Console.WriteLine(x); };
            // 调用委托。
            md1("C#");
            md2("Java");
            md3("Python");
            Console.ReadKey();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/130979294