网站建设重要性,黄岐建网站,设计集团有限公司,深圳自适应网站开发公司“Lambda 表达式”是一个匿名函数#xff0c;它可以包含表达式和语句#xff0c;并且可用于创建委托或表达式目录树类型。 实例如下#xff1a; 代码如下#xff1a;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.… “Lambda 表达式”是一个匿名函数它可以包含表达式和语句并且可用于创建委托或表达式目录树类型。 实例如下 代码如下
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp2
{delegate bool D();delegate bool D2(int i);class Test{D del;D2 del2;public void TestMethod(int input){int j 0;// Initialize the delegates with lambda expressions.// Note access to 2 outer variables.// del will be invoked within this method.del () { j 10; return j input; };// del2 will be invoked after TestMethod goes out of scope.del2 (x) { return x j; };// Demonstrate value of j:// Output: j 0 // The delegate has not been invoked yet.Console.WriteLine(调用前j {0}, j);// Invoke the delegate.bool boolResult del();// Output: j 10 b TrueConsole.WriteLine(调用后j {0}. b {1}, j, boolResult);}static void Main(){Test test new Test();test.TestMethod(5);// Prove that del2 still has a copy of// local variable j from TestMethod.bool result test.del2(10);// Output: TrueConsole.WriteLine(result);Console.ReadKey();}}}