委托是对函数的封装,可以当做给方法的特征指定一个名称。它本质上就是变相的类型安全的指针。
假想场景:01国际计算机语言大会上,各种计算机语言汇聚一堂,共同探讨计算机语言的发展趋势和优势。由于语言众多,主办方pc和mac决定给每位参会者10分钟发言机会。Code is cheap。看看它们都怎么说:
类图:
代码如下:
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace CsStudy
{
class DelegateStudy
{
interface IProgramLanguage
{
void Speak();
}
class Java : IProgramLanguage
{
public void Speak()
{
Console.WriteLine("Java is the best.");
}
}
class JavaScript : IProgramLanguage
{
public void Speak()
{
Console.WriteLine("JavaScript is the best.");
}
}
class C : IProgramLanguage
{
public void Speak()
{
Console.WriteLine("C is the best.");
}
}
class CSharp : IProgramLanguage
{
public void Speak()
{
Console.WriteLine("Oh really? C# is the best of best.");
}
}
public delegate void LanguageSpeak(); //声明委托
LanguageSpeak programLs;
public void BeginSpeak()
{
Console.WriteLine("Are you ready? Go!");
if (programLs != null)
{
programLs();
}
}
/// <summary>
/// 客户端调用
/// </summary>
static void Main(string[] args)
{
DelegateStudy ds = new DelegateStudy();
ds.programLs += new DelegateStudy.LanguageSpeak(new Java().Speak);
ds.programLs += new DelegateStudy.LanguageSpeak(new JavaScript().Speak);
ds.programLs += new DelegateStudy.LanguageSpeak(new C().Speak);
ds.programLs += new DelegateStudy.LanguageSpeak(new CSharp().Speak);
ds.BeginSpeak();
Console.Read();
}
}
}
委托总结:
(1)与命名方法关联时委托在语法上总是带有一个参数的构造函数,这个参数就是委托引用的方法,这个方法必须匹配最初定义委托时的签名,即方法返回值类型和参数序列必须都相匹配。
(2)通过将委托与命名方法(静态或实例)或匿名方法关联,可以实例化委托。委托的实例仍称为委托。
(3)使用委托即通过委托的实例调用委托引用的方法,委托的实例名后面的括号包含的参数与委托引用的方法参数匹配。
(4)委托可以包含多个方法,这种委托称为多播委托。多播委托只能引用返回值为void的方法。运算符有+、+=、-、-=,用来增加方法和删除方法。
(5)委托使用三步曲:定义->关联->调用
Code
public delegate void LanguageSpeak(); //定义
LanguageSpeak programLs = new LanguageSpeak(new CSharp().Speak); //关联
programLs(); //调用
最后来一个带参数的委托调用:
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace CsStudy
{
using System;
class DelegateStudy
{
interface IProgramLanguage
{
string Speak(string language);
}
class Java : IProgramLanguage
{
public string Speak(string language)
{
return string.Format( "{0} is the best.",language);
}
}
class JavaScript : IProgramLanguage
{
public string Speak(string language)
{
return string.Format("{0} is the best.", language);
}
}
class C : IProgramLanguage
{
public string Speak(string language)
{
return string.Format("{0} is the best.", language);
}
}
class CSharp : IProgramLanguage
{
public string Speak(string language)
{
return string.Format("{0} is the best.", language);
}
}
public delegate string LanguageSpeak(string language); //声明委托
LanguageSpeak programLs;
/// <summary>
/// 客户端调用
/// </summary>
static void Main(string[] args)
{
DelegateStudy ds = new DelegateStudy();
ds.programLs = new DelegateStudy.LanguageSpeak(new Java().Speak);
Console.WriteLine(ds.programLs("Java"));
ds.programLs += new DelegateStudy.LanguageSpeak(new JavaScript().Speak);
Console.WriteLine(ds.programLs("JavaScript"));
ds.programLs += new DelegateStudy.LanguageSpeak(new C().Speak);
Console.WriteLine(ds.programLs("C"));
ds.programLs += new DelegateStudy.LanguageSpeak(new CSharp().Speak);
Console.WriteLine(ds.programLs("C#"));
Console.Read();
}
}
}