在C#中使用委托调用成员函数
先决条件:C#中的代表
我们还可以使用委托来调用类的成员函数。它类似于静态函数调用,这里我们必须在创建委托时使用对象传递成员函数。
程序:
using System;
using System.Collections;
public delegate void myDelegates();
class Sample
{
public void fun()
{
Console.WriteLine("Call a member function using delegate");
}
}
class Program
{
static void Main()
{
Sample S = new Sample();
myDelegates del = new myDelegates(S.fun);
del();
}
}输出结果
Call a member function using delegate
在上面的示例中,我们创建了Sample类,Sample类包含一个成员函数namefun()。然后,我们创建了另一个非包含Main()函数的类Program。在这里,我们创建了委托引用,并使用Sample类对象传递了成员函数。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短