Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
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
Posted

Read this article.

Delegate

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

Regards
Ankit
 
Share this answer
 
Comments
Nish Nishant 21-Apr-11 8:52am    
Voted 5!
M.CHAITHANYA 21-Apr-11 9:00am    
Hi Ankit,
Cant we assign address of a method to a class variable .If so how and what happens if we do this?

Regards
Chaithanya M
BobJanova 21-Apr-11 9:13am    
No. Delegates are how runtime method selection is done in C#. They are essentially managed function pointers. Read the article.
Sergey Alexandrovich Kryukov 21-Apr-11 14:46pm    
READ IT!!!!!!!!!!!!!!!!!!!
Shall I say it again?
There are no "addresses". This is a managed platform.
--SA
Sergey Alexandrovich Kryukov 21-Apr-11 14:47pm    
My 5. This is the solution.
--SA
C# has delegates for this, see Delegates (C# Programming Guide)[^].
 
Share this answer
 
Comments
Nish Nishant 21-Apr-11 8:52am    
Voted 5!
CPallini 21-Apr-11 8:59am    
Thank you. :-)
Sergey Alexandrovich Kryukov 21-Apr-11 14:47pm    
Same solution, a 5.
--SA
CPallini 21-Apr-11 15:36pm    
Thanks!
Sergey Alexandrovich Kryukov 21-Apr-11 14:53pm    
I also added the clarification on the nature of delegates: why it is not the "address".
Please see.
--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
 
Share this answer
 
Comments
CPallini 21-Apr-11 15:35pm    
Way too technical for me :-D 5.
Sergey Alexandrovich Kryukov 21-Apr-11 15:45pm    
...cannot believe that. How it can be "too technical"? :-)
Thank you very much,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900