静态方法代理:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
delegate
void
DoGreeting(
string
name);
class Program
{
[STAThread]
static void Main( string [] args)
{
// 方法名当成参数传给委托类型调用
MarkGreeting( " 张三 " , GreetingEnglish);
MarkGreeting( " 李四 " , GreetingChinese);
}
/// <summary>
/// 委托(代理的方法)
/// </summary>
/// <param name="name"></param>
/// <param name="markGreeting"></param>
private static void MarkGreeting( string name, DoGreeting markGreeting)
{
markGreeting(name);
}
/// <summary>
/// 用英文打招呼
/// </summary>
/// <param name="name"></param>
private static void GreetingEnglish( string name)
{
Console.WriteLine(name + " hello! " );
}
/// <summary>
/// 用中文打招呼
/// </summary>
/// <param name="name"></param>
private static void GreetingChinese( string name)
{
Console.WriteLine(name + " 你好! " );
}
}
class Program
{
[STAThread]
static void Main( string [] args)
{
// 方法名当成参数传给委托类型调用
MarkGreeting( " 张三 " , GreetingEnglish);
MarkGreeting( " 李四 " , GreetingChinese);
}
/// <summary>
/// 委托(代理的方法)
/// </summary>
/// <param name="name"></param>
/// <param name="markGreeting"></param>
private static void MarkGreeting( string name, DoGreeting markGreeting)
{
markGreeting(name);
}
/// <summary>
/// 用英文打招呼
/// </summary>
/// <param name="name"></param>
private static void GreetingEnglish( string name)
{
Console.WriteLine(name + " hello! " );
}
/// <summary>
/// 用中文打招呼
/// </summary>
/// <param name="name"></param>
private static void GreetingChinese( string name)
{
Console.WriteLine(name + " 你好! " );
}
}
实例方法代理:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
delegate
void
DoGreeting(
string
name);
public class Greeting
{
/// <summary>
/// 委托(代理的方法)
/// </summary>
/// <param name="name"></param>
/// <param name="markGreeting"></param>
public void MarkGreeting( string name, DoGreeting markGreeting)
{
markGreeting(name);
}
/// <summary>
/// 用英文打招呼
/// </summary>
/// <param name="name"></param>
public void GreetingEnglish( string name)
{
Console.WriteLine(name + " hello! " );
}
/// <summary>
/// 用中文打招呼
/// </summary>
/// <param name="name"></param>
public void GreetingChinese( string name)
{
Console.WriteLine(name + " 你好! " );
}
}
class Program
{
[STAThread]
static void Main( string [] args)
{
Greeting greetingObj = new Greeting();
greetingObj.MarkGreeting( " 张三 " , greetingObj.GreetingEnglish);
greetingObj.MarkGreeting( " 李四 " , greetingObj.GreetingChinese);
}
public class Greeting
{
/// <summary>
/// 委托(代理的方法)
/// </summary>
/// <param name="name"></param>
/// <param name="markGreeting"></param>
public void MarkGreeting( string name, DoGreeting markGreeting)
{
markGreeting(name);
}
/// <summary>
/// 用英文打招呼
/// </summary>
/// <param name="name"></param>
public void GreetingEnglish( string name)
{
Console.WriteLine(name + " hello! " );
}
/// <summary>
/// 用中文打招呼
/// </summary>
/// <param name="name"></param>
public void GreetingChinese( string name)
{
Console.WriteLine(name + " 你好! " );
}
}
class Program
{
[STAThread]
static void Main( string [] args)
{
Greeting greetingObj = new Greeting();
greetingObj.MarkGreeting( " 张三 " , greetingObj.GreetingEnglish);
greetingObj.MarkGreeting( " 李四 " , greetingObj.GreetingChinese);
}