1.定义的委托和方法

    delegate void TestDelegate(string s);
    static void M(string s)
    {
        Console.WriteLine(s);
    }

2.常规委托:

// Original delegate syntax required 
        // initialization with a named method.
        TestDelegate testdelA = new TestDelegate(M);

3.匿名方法

 

 // C# 2.0: A delegate can be initialized with
        // inline code, called an "anonymous method." This
        // method takes a string as an input parameter.
        TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };

 

4.Lambda 表达式

 // C# 3.0. A delegate can be initialized with
        // a lambda expression. The lambda also takes a string
        // as an input parameter (x). The type of x is inferred by the compiler.
        TestDelegate testDelC = (x) => { Console.WriteLine(x); };

 

 

相关文章:

  • 2021-10-03
  • 2021-07-27
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-03
  • 2021-05-31
  • 2021-10-11
  • 2022-02-18
  • 2021-09-14
相关资源
相似解决方案