典型的委托的不同写法(转载)

         典型的委托的不同写法。

public delegate void TheEvent(int a);
public void test()
{
 TheEvent testdel1 = new TheEvent(del1);
 testdel1(12);
}

public void del1(int x)
{
 Console.WriteLine("output x : {0}", x);
}

  现在我们可以写成这样:

public void test()
{
 TheEvent testdel1 = del1;
 testdel1(12);
}

  或者将程序改写为:

delegate void TheEvent2(int a);
public void test2()
{
 int a = 12;
 TheEvent ev2 = delegate(ref int x)
 { Console.WriteLine("output x : {0}", x); };
 ev2( ref a);
}

  如下代码将是委托本质的提高?让我们看看下面的例子。

public static void test3()
{
 int a = 12;
 int y = 32;
 TheEvent ev2 = delegate(ref int x)
 { Console.WriteLine("output x + y : {0}", x + y); };
 ev2( ref a);
}
原文地址:https://www.cnblogs.com/xiaobaigang/p/840522.html