将方法的地址关联到变量. [英] Assing address of a method to a variable.

查看:63
本文介绍了将方法的地址关联到变量.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

假设类"A"具有方法getsum().如果我们想获取getsum()方法的地址并将其分配给引用类型的变量,方法是这样的:


一个a = getsum; --------就像我们不包含地址一样,该方法的地址将被分配给变量.这是真的吗?






问候
Chaithanya M

Hi Everyone,

Suppose Class "A" has a method getsum() .If we want to get the address of the getsum() method and assign it to a variable of reference type..is this the way:


A a=getsum;----Is it like if we dont include the address ,the address of that method will be assigned to teh variable.Is this true?






Regards
Chaithanya M

推荐答案

阅读本文.

委托

您需要使用委托来处理函数地址.

问候
Ankit
Read this article.

Delegate

You need to use the delegates for handling a function address.

Regards
Ankit


C#具有为此的委托,请参见 ^ ].
C# has delegates for this, see Delegates (C# Programming Guide)[^].


只需添加一些说明即可.

代表不是参考或地址.在OOP和实例方法中,委托中使用的方法不是一个地址".它有两个:一个是地址"(如果是入口点),另一个是"this"(对方法调用中使用的类的实例的引用).如果不是"this",则方法将如何访问实例成员.您也可以在委托中使用静态方法,因此"this"可以为null(未使用).

现在,一个委托实例甚至不是一对地址".如果使用Reflection查看一个实例,您将发现它是一个类实例.此类实现调用列表,因此可以添加多个处理程序.

—SA
Just to add some clarification.

Delegates are not references or addresses. In OOP and instance method, a method used in delegate is not one "address". It''s two: one is the "address" if entry point, another is "this", a reference to instance of the class used in a method call. If not "this", how a method would access the instance member. You can use static methods in delegates as well, so "this" can be null (unused).

Now, a delegate instance is not even a pair of "addresses". If you look at one using Reflection, you will find that it is a… a class instance. This class implements the invocation lists, so multiple handlers can be added.

—SA


这篇关于将方法的地址关联到变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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