C#中的委托
自学的过程中,一直没跨过委托这个槛儿,今天刚好有点空,拿起书对照着MSDN再看了一遍。简要记录一二,以供后续温习之便。
委托入门
使用委托的规则如下:首先使用 delegate 关键字声明一个委托类型,指定其返回类型和参数。通过 new 操作符,可以使用委托声明来创建一个委托类型的实例。在创建委托类型的实例时,目标方法必须与委托声明的签名精确匹配。
namespace Hans_GIS.DelegateExample01
{
class Program
{
static void Main(string[] args)
{
int temp;
int[] array = new int[]{ 23, 28, 24, 26, 25 };
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (Greater(array[i], array[j]))
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
foreach (int i in array)
{
Console.Write("{0} ", i);
}
Console.ReadKey();
}
static bool Greater(int iNum1, int iNum2)
{
if (iNum1 > iNum2)
{
return true;
}
else
{
return false;
}
}
}
}
namespace Hans_GIS.DelegateExample01
{
delegate bool Compare(int i1,int i2);
class Program
{
static void Main(string[] args)
{
Compare com = new Compare(Greater);
int temp;
int[] array = new int[]{ 23, 28, 24, 26, 25 };
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (com(array[i], array[j]))
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
foreach (int i in array)
{
Console.Write("{0} ", i);
}
Console.ReadKey();
}
static bool Greater(int iNum1, int iNum2)
{
if (iNum1 > iNum2)
{
return true;
}
else
{
return false;
}
}
}
}
每个委托都有自己的签名,例如上面的示例中委托声明:delegate bool Compare(int i1,int i2),这里所说的签名就是指该委托有两个int类型的参数,还有一个bool类型的返回值。在实例化委托时,需要为其分配一个方法,将其作为其构造函数的参数,如 Compare com = new Compare(Greater),在这里,Compare委托的实例com引用了Greater方法,调用com的时也就会调用Greater方法了。值得注意的是,该方法必须与委托声明具有同样的签名。如上例中的static bool Greater(int iNum1, int iNum2)。
多路广播委托
调用委托时,它可以调用多个方法,这称之为多路广播委托。可以使用加法运算符或加法赋值运算符(“+”或“+=”)向委托的方法列表(调用列表)中添加额外的方法;可以使用减法运算符或减法赋值运算符(“-”或“-=”)从调用列表中移除方法。
namespace Hans_GIS.DelegateExample02
{
class Program
{
delegate void UppercaseDelegate(string input);
static void UppercaseFirst(string input)
{
char[] buffer = input.ToCharArray();
buffer[0] = char.ToUpper(buffer[0]);
WriteOutput(input, new string(buffer));
}
static void UppercaseLast(string input)
{
char[] buffer = input.ToCharArray();
buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
WriteOutput(input, new string(buffer));
}
static void UppercaseAll(string input)
{
WriteOutput(input, input.ToUpper());
}
static void WriteOutput(string input, string strRe)
{
Console.WriteLine("Your string before: {0}", input);
Console.WriteLine("Your string after: {0}", strRe);
}
static void Main()
{
UppercaseDelegate uppercase = new UppercaseDelegate(UppercaseFirst);
uppercase += new UppercaseDelegate(UppercaseLast);
uppercase += UppercaseAll;
uppercase("hans_gis");
Console.ReadKey();
}
}
}
结果输出:

委托的异步回调
既然委托实例是一个对象,那么就可以将其作为参数进行传递,也可以将其赋值给属性。如此,方法便可以接受一个委托参数,并且之后可以调用该委托,这称之为异步回调。
对上面多路广播委托的代码做了一点改动,如下:
namespace Hans_GIS.DelegateExample03
{
class Program
{
delegate string UppercaseDelegate(string input);
static string UppercaseFirst(string input)
{
char[] buffer = input.ToCharArray();
buffer[0] = char.ToUpper(buffer[0]);
return new string(buffer);
}
static string UppercaseLast(string input)
{
char[] buffer = input.ToCharArray();
buffer[buffer.Length - 1] = char.ToUpper(buffer[buffer.Length - 1]);
return new string(buffer);
}
static string UppercaseAll(string input)
{
return input.ToUpper();
}
static void WriteOutput(string input, UppercaseDelegate del)
{
Console.WriteLine("Your string before: {0}", input);
Console.WriteLine("Your string after: {0}", del(input));
}
static void Main()
{
WriteOutput("hans_gis", new UppercaseDelegate(UppercaseFirst));
WriteOutput("hans_gis", new UppercaseDelegate(UppercaseLast));
WriteOutput("hans_gis", new UppercaseDelegate(UppercaseAll));
Console.ReadKey();
}
}
}
输出结果同上。
出处:http://www.cnblogs.com/hans_gis/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号