委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。
1.委托的声明
(1). delegate
delegate我们常用到的一种声明
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。
例:
public delegate int MethodtDelegate(int x, int y);表示有两个参数,并返回int型。
(2). Action
Action是无返回值的泛型委托。
Action 表示无参,无返回值的委托
Action<int,string> 表示有传入参数int,string无返回值的委托
Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action至少0个参数,至多16个参数,无返回值。
(3). Func
Func是有返回值的泛型委托
Func<int> 表示无参,返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托
Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void
using System;
namespace wt
{
class Program
{
public delegate void Operate(int a, int b);
static void Main(string[] args)
{
Operating(Sub, 3, 5);
Console.ReadKey();
}
public static void Add(int a, int b)
{
Console.WriteLine(a + b);
}
public static void Sub(int a, int b)
{
Console.WriteLine(a - b);
}
public static void Mul(int a, int b)
{
Console.WriteLine(a * b);
}
public static void Div(int a, int b)
{
Console.WriteLine(a / b);
}
public static void Operating(Operate p, int a, int b)
{
p(a, b);
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------
using System;
namespace Delegates
{
public class EventDate : EventArgs
{
public string name;
public int age;
}
public delegate void doHandle(object sender, EventDate e); //委托类型的定义
class Program
{
static void Main(string[] args)
{
A a = new A(); //声明委托变量名
B b = new B();
a.myEvent += new doHandle(b.f1); //委托增加方法
a.myEvent += new doHandle(b.f2);
a.callIt(Console.ReadLine(), new EventDate());
}
}
class A
{
public event doHandle myEvent;
public void callIt(string qianzui, EventDate e)
{
e.name = qianzui + "疯子";
e.age = 10;
//在委托前,判断委托是否为空(调用为空会抛出异常)
if (null != myEvent)
{
myEvent(this, e);
}
}
}
class B
{
public void f1(object sender, EventDate e)
{
Console.WriteLine("姓名" + e.name);
}
public void f2(object sender, EventDate e)
{
Console.WriteLine("年龄" + e.age);
Console.Read();
}
}
}
------------------------------------------------------------------------------------------------------------------------------
(2). Action
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
b.myAction += a.MyFunc1;
b.myAction += a.MyFunc2;
b.Call();
Console.ReadLine();
}
}
class A
{
public void MyFunc1(string s)
{
Console.WriteLine("I love u " + s);
}
public void MyFunc2(string s)
{
Console.WriteLine("I Like u " + s);
}
}
class B
{
public Action<string> myAction;
public void Call()
{
if (myAction != null)
{
myAction(Console.ReadLine());
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------
(3). Func
using System;
using System.Collections.Generic;
using System.Text;
namespace Func
{
class Program
{
static void Main(string[] args)
{
A a = new A();
B b = new B();
b.myFuncs += a.myFunc1;
b.myFuncs += a.myFunc2;
b.Call();
Console.ReadLine();
}
}
class A
{
public string myFunc1(string s)
{
Console.WriteLine(s+1);
return s;
}
public string myFunc2(string s)
{
Console.WriteLine(s+33);
return s ;
}
}
class B
{
public Func<string, string> myFuncs;
public void Call()
{
if (myFuncs != null)
{
myFuncs((Console.ReadLine()));
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------
委托回调分析
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Delegate_HR
{
class Program
{
static void Main(string[] args)
{
GBY gby = new GBY();
ZTL ztl = new ZTL();
Hr hr = new Hr(new LJY());
hr.CommandLJY();
Console.Read();
}
}
class Hr
{
LJY ljy;
public Hr(LJY ljy)
{
this.ljy = ljy;
}
public void CommandLJY()
{
if(ljy == null)
{
Console.WriteLine("mdl没上班");
return;
}
ljy.Call();
}
}
public delegate void CallSomeBody();
class LJY
{
public static CallSomeBody csb;
public void Call()
{
if (csb != null)
csb();
}
}
class ZTL
{
public ZTL()
{
LJY.csb += AnswerCall;
}
void AnswerCall()
{
Console.WriteLine("我是mdlA,我好高兴接到你的电话");
}
}
class GBY
{
public GBY()
{
LJY.csb += AnswerCall;
}
void AnswerCall()
{
Console.WriteLine("我是mdlB,我好高兴接到你的电话");
}
}
}