最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
初探.NET中的delegate类型与.NET事件
时间:2022-07-02 10:53:30 编辑:袖梨 来源:一聚教程网
1.了解delegate
delegate可以认为是一种函数的指针,一个delegate类型的实例代表可以代表一个方法,在实际使用中我们可以在不知道方法名称的情况下调用到这个方法。前面说太多可能会使大家变得糊涂,或者摸不着头脑,我举个例子,来详细说明此种类型的用法:
整个例程的代码,是控制台项目.
//代码开始
using System;
namespace ConsoleApp1
{
public delegate string FunctionPointer(string strName);
class Class1
{
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
System.Console.WriteLine("Hello World!");
FunctionPointer fpa = new FunctionPointer(aFunction);
FunctionPointer fpb = new FunctionPointer(bFunction);
InvokeMethod(fpa);
InvokeMethod(fpb);
System.Console.ReadLine();
//
}
public static string aFunction(string sNameA)
{
return "aFunction say i'm " + sNameA;
}
public static string bFunction(string sNameB)
{
return "bFunction say i'm " + sNameB;
相关文章
- 墨西哥酒店巨头 Murano 启动5亿美元比特币储备计划:支援客户 BTC 支付房费 07-08
- 炉石传说英雄乱斗角斗场乱斗卡组分享 07-08
- Dreamweaver怎么使用标签及代码设计表格? 07-08
- 无畏契约大圆点准星代码分享 07-08
- 不会程式就夺冠!他靠AI「Vibe Coding」横扫200场黑客松战场 07-08
- 云顶之弈强音争霸8比特女警阵容搭配指南 07-08