1.泛型委托
“泛型类型”是可适应对多种数据类型执行相同功能的单个编程元素。定义泛型类或过程时,无需为可能需要对其执行该功能的每个数据类型定义单独版本。
就好比是带有可拆卸刀头的螺丝刀。您检查需要拧动的螺丝,然后选择适合该螺丝的刀头(一字、十字、星形)。将正确的刀头插入到螺丝刀柄上后,您就可以使用螺丝刀执行完全相同的功能,即拧螺丝。螺丝刀就是泛型工具!
定义泛型类型时,即使用一个或多个数据类型将其参数化。这样可允许使用代码定制数据类型以满足其要求。代码可以通过泛型元素声明若干个不同的编程元素,每个元素可使用一组不同的数据类型。但是,无论声明的元素使用哪些数据类型,它们均执行相同的逻辑。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleTry
{
public delegate string SampleDelegate<T,S>(T s1,S s2);
class Program
{
/// <summary>
/// Main Method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
SampleDelegate<string, int> d1 = new SampleDelegate<string, int>(new Test().AddStr);
Console.Write(d1("My age is ", 23));
Console.Read();
}
}
class Test
{
/// <summary>
/// Test Method
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string AddStr(string s1, int s2)
{
return s1 + s2;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleTry
{
public delegate string SampleDelegate<T,S>(T s1,S s2);
class Program
{
/// <summary>
/// Main Method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
SampleDelegate<string, int> d1 = new SampleDelegate<string, int>(new Test().AddStr);
Console.Write(d1("My age is ", 23));
Console.Read();
}
}
class Test
{
/// <summary>
/// Test Method
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string AddStr(string s1, int s2)
{
return s1 + s2;
}
}
}
2.委托与事件
事件是一个信号,它告知应用程序有重要情况发生。例如,用户单击窗体上的某个控件时,窗体可能会引发一个 Click 事件并调用一个处理该事件的过程。事件还允许在不同任务之间进行通信。比方说,您的应用程序脱离主程序执行一个排序任务。若用户取消这一排序,应用程序可以发送一个取消事件让排序过程停止。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleTry
{
public delegate void ProcessDelegate(object sender, EventArgs e);
class Program
{
/// <summary>
/// Main Method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Test t = new Test();
t.ProcessEvent += new ProcessDelegate(t_ProcessEvent);
Console.WriteLine(t.Process());
Console.Read();
}
static void t_ProcessEvent(object sender, EventArgs e)
{
Test t = (Test)sender;
t.Text1 = "Hello";
t.Text2 = "World";
}
}
class Test
{
private string s1;
public string Text1
{
get { return s1; }
set { s1 = value; }
}
private string s2;
public string Text2
{
get { return s2; }
set { s2 = value; }
}
public event ProcessDelegate ProcessEvent;
public string Process()
{
if (ProcessEvent == null)
ProcessEvent += new ProcessDelegate(t_ProcessEvent);
ProcessEvent(this, EventArgs.Empty);
return s1 + s2;
}
void t_ProcessEvent(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
using System.Collections.Generic;
using System.Text;
namespace ConsoleTry
{
public delegate void ProcessDelegate(object sender, EventArgs e);
class Program
{
/// <summary>
/// Main Method
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Test t = new Test();
t.ProcessEvent += new ProcessDelegate(t_ProcessEvent);
Console.WriteLine(t.Process());
Console.Read();
}
static void t_ProcessEvent(object sender, EventArgs e)
{
Test t = (Test)sender;
t.Text1 = "Hello";
t.Text2 = "World";
}
}
class Test
{
private string s1;
public string Text1
{
get { return s1; }
set { s1 = value; }
}
private string s2;
public string Text2
{
get { return s2; }
set { s2 = value; }
}
public event ProcessDelegate ProcessEvent;
public string Process()
{
if (ProcessEvent == null)
ProcessEvent += new ProcessDelegate(t_ProcessEvent);
ProcessEvent(this, EventArgs.Empty);
return s1 + s2;
}
void t_ProcessEvent(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
}
}