这两天深入研究了一下代理和事件。下面是关于代理的学习笔记,关于代理的多点传送的例子:
using System;
using System.Collections.Generic;
using System.Text;
/*--------------代理的多点传送:-----------------
* 1)多点传送的基本方法:+=和-= 构造代理链表
* 2)注意事项:代理必须具有void返回类型
-----------------------------------------------*/
namespace ConsoleApplication11
{
delegate void StrMod(ref string str);
class Program
{
static void Main(string[] args)
{
string str = "hello world! /n hello everyone";
StrMod strOp;
strOp = new StrMod(StringHandler.RemoveSpaces);
strOp += new StrMod(StringHandler.Reverse);
//strOp -= new StrMod(StringHandler.Reverse);
strOp(ref str);
Console.WriteLine(str);
}
}
public class StringHandler
{
public static void RemoveSpaces(ref string str)
{
string temp = "";
for (int i = 0; i < str.Length; i++)
{
if (str[i] != ' ')
temp += str[i];
}
str = temp;
}
public static void Reverse(ref string str)
{
string temp = "";
for (int i = str.Length - 1; i >= 0; i--)
{
temp += str[i];
}
str = temp;
}
}
}