谁能指点一下C#中委托的概念?? 交流交流.

Mynetstart 2003-02-05 11:26:39
//有关C#中委托本人觉得有点迷惑,那位高手能指点一下,作为交流,谢谢!! 以下为两个例子,其中一个采用委托,
//另一个没有.

//以下是采用委托.
using System;

namespace Test
{

public class Ctest
{
int n,m;
public delegate int Op(int a, int b); //委托.
public Ctest(int a, int b)
{
n=a;
m=b;
}
public int DoOp(Op ar)
{
return ar(n,m);
}
}
public class DoTest
{
public int AddOp(int a, int b)
{
return a+b;
}
public static int SubOp(int a, int b)
{
return a-b;
}


[STAThread]
public static void Main(string[] args)
{
DoTest dt=new DoTest();
Ctest.Op add=new Ctest.Op(dt.AddOp );
Ctest.Op sub=new Ctest.Op(DoTest.SubOp );

Ctest at= new Ctest(5,3);

int reOne=at.DoOp (add);
int reTwo=at.DoOp (sub);

Console.WriteLine ("The first result is " + reOne);
Console.WriteLine ("The second result is " + reTwo);

}
}
}


//以下代码没有委托,输出一样.
using System;

namespace Test
{


public class DoTest
{

public int AddOp(int a, int b)
{
return a+b;
}
public static int SubOp(int a, int b)
{
return a-b;
}


[STAThread]
public static void Main(string[] args)
{

DoTest at= new DoTest();
int reOne=at.AddOp(5,3);
int reTow=at.SubOp(5,3);

Console.WriteLine ("The first result is " + reOne);
Console.WriteLine ("The second result is " + reTwo);

}
}
}

...全文
44 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
look4u 2003-05-13
  • 打赏
  • 举报
回复
learing
yarshray 2003-02-06
  • 打赏
  • 举报
回复
函数指针

参考如下文章:

http://www.csdn.net/develop/read_article.asp?id=16267

http://www.csdn.net/develop/read_article.asp?id=16260
Mynetstart 2003-02-05
  • 打赏
  • 举报
回复
究竟在C#中的委托的真正作用是什么? 在工程中如何利用它???

110,523

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧