program.cs
来自「csharp课本的源代码」· CS 代码 · 共 35 行
CS
35 行
using System;
namespace DelegateExample
{
//第一步:声明委托
public delegate string MyDelegate(string name);
public class Program
{
//第二步:定义被调用的方法
public static string FunctionA(string name)
{
return "A say Hello to " + name;
}
public static string FunctionB(string name)
{
return "B say Hello to " + name;
}
//第三步:定义delegate类型的处理函数,并在此函数中
// 通过delegate类型调用步骤定义的方法
public static void MethodA(MyDelegate Me)
{
Console.WriteLine(Me("张三"));
}
public static void Main()
{
//第四步:创建实例,传入准备调用的方法名
MyDelegate a = new MyDelegate(FunctionA);
MyDelegate b = new MyDelegate(FunctionB);
MethodA(a);
MethodA(b);
//按回车键结束
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?