调用方法与事件(click();)之间的区别,并在示例中以递归方法(hmos();)调用它 [英] difference between calling method with event( click();) and calling it in recursive method (hmos();) in the example

查看:63
本文介绍了调用方法与事件(click();)之间的区别,并在示例中以递归方法(hmos();)调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace farag

{

public delegate void mydelegate();

公共课程受到影响

{

//公共事件mydelegate点击;

public void dosomthing()

{

hmos(); //或点击();

}

public void hmos()

{

Console.WriteLine(" \ nn \ nn \\ tt \\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\空格:pre"> }

类程序

{
$


static void Main(string [] args)

{

hamed h = new hamed();

// h.click  + = new mydelegate(h.hmos);

h.dosomthing();



Console.ReadKey();

}

}




namespace farag
{
public delegate void mydelegate();
public class hamed
{
//public event mydelegate click;
public void dosomthing()
{
hmos();// or click();
}
public void hmos()
{
Console.WriteLine("\n\n\n\t\t\tallah akbar");
}
}
class Program
{

static void Main(string[] args)
{
hamed h = new hamed();
//h.click  += new mydelegate(h.hmos);
h.dosomthing();

Console.ReadKey();
}
}


推荐答案

没有区别。程序指令在内存中,必须有方法表。当处理器处理程序代码并且存在方法调用时,它需要知道方法的代码在内存中的哪个位置来读取和处理它。委托
类似于指向方法的指针,因此它可以在方法块启动的内存中保存位置。它是如何工作的非常简单的观点。

There are no differences. Program instructions are in memory and there must be something like methods table. As processor processes program code and there is method calls it needs to know where code of method is in memory to read and process it. Delegate is something like pointer to method so it saves place in memory where method block starts. It is very easy point of view how it works.

C中方法指针的用途是关于方法的快捷方式。在C#中有更多的利用率。使用委托的主要目的是实现观察者模式。如果您想知道是否有某些新闻/更改,则无法调用其他部分代码。即使您的方法是私有的,另一部分
的代码也可以调用您的代码。 

Purpose of method pointer in C is about shortcut to method. In C# there is more utilization. Main aim to use delegates is implementation observer pattern. You cannot call another part of code if you would to know if there are some news/changes. Another part of code can call your code even when your method is private. 


这篇关于调用方法与事件(click();)之间的区别,并在示例中以递归方法(hmos();)调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆