您的位置:首页 > 编程语言 > C#

c# 多播委托 和匿名方法

2010-02-21 15:41 363 查看
using System;
using System.Collections.Generic;
using System.Text;

namespace threadserver_c
{
delegate void StrMod(ref string str);
class DelegateTest
{
public  void  replaceSpaces(ref string a)
{
System.Windows.Forms.MessageBox.Show("Replaces spaces with hyphens.");
a= a.Replace(' ', '-');
}
public  void  removeSpaces(ref string a)
{
string temp = "";
int i;
System.Windows.Forms.MessageBox.Show("Removing spaces.");
for (i = 0; i < a.Length; i++)
{
if (a[i] != ' ') temp += a[i];
}
a= temp;
}
static string reverse(string a)
{
string temp = "";
int i, j;
System.Windows.Forms.MessageBox.Show("reversing string.");
for (j = 0, i = a.Length - 1; i >= 0; i--, j++)
{
temp += a[i];
}

return temp;
}

}
}

private void Form1_Load(object sender, EventArgs e)
{
DelegateTest a = new DelegateTest();
StrMod strop;
StrMod replaceSp= a.replaceSpaces;
StrMod removeSp = a.removeSpaces;
string str="This is a test.";
strop = replaceSp;
strop += removeSp;
strop(ref str  );
MessageBox.Show(str);
}

private void button1_Click(object sender, EventArgs e)
{
CountIt count = delegate
{
for (int i = 0; i < 5; i++)
this.Text = i.ToString ();
};
count();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: