本文主要是介绍C#委托处理方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
delegate double MathAction(double num);class DelegateTest
{
// Regular method that matches signature:
//定义静态方示(传入参数)
static double Double(double input)
{
return input * 2;
}
static void Main()
{
// Instantiate delegate with named method:
//实例化一个委托并指定处理方法
MathAction ma = Double;
//MathAction MA1 = Double;
// 定义一参数接收委托事件传入一个参数给 返回值方法
// Invoke delegate ma:
double multByTwo = ma(4.5);
Console.WriteLine("multByTwo: {0}", multByTwo);
//实例化事件 {}是指定理方法
// Instantiate delegate with anonymous method:
MathAction ma2 = delegate(double input)
{
return input * input;
};
double square = ma2(5);
Console.WriteLine("square: {0}", square);
// Instantiate delegate with lambda expression
//LamaD表达式
MathAction ma3 = s => s * s * s;
double cube = ma3(4.375);
Console.WriteLine("cube: {0}", cube);
Console.ReadKey();
Delegate1.Del_Name a = Delegate1.result;
int b=a(5);
Console.WriteLine("{0}", b);
Console.ReadKey();
Delegate1.Del_Name a1 = delegate(int b1)
{
return b1 + 45;
};
a1(78);
Console.WriteLine("{0}",a1(78));
Console.ReadKey();
Delegate1.Del_Name c1 = d1 =>d1 + 55 + d1;
c1(6);
Console.WriteLine("{0}", c1(78));
Console.ReadKey();
}
// Output:
// multByTwo: 9
// square: 25
// cube: 83.740234375
}
static class Delegate1
{
public delegate int Del_Name(int a);
public static int result(int a)
{
int b = a * a + 5;
return b;
}
}
class myclass
{
}
这篇关于C#委托处理方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!