Click here to Skip to main content
15,896,486 members
Please Sign up or sign in to vote.
1.85/5 (5 votes)
See more:
Please tell me the answer for my question.
Posted
Updated 17-Oct-17 0:08am
v3
Comments
kanmaniponniah 3-Mar-12 1:39am    
thank you .It is use full to me

Delegates provide additional level of abstraction, because you can abstract out some action expressed in the code and pass is as a parameter in some other method, like any other parameter.
This is called "first-class citizen".

OOP virtual methods and late binding does something like that (and this is also a method parameter, hidden on), but delegated has no limitations: delegate instances can accept any method in its invocation list; if the method has matching signature it can be virtual or not, static or instance method, of the same class or any other (which is the main difference between a delegate and a virtual method).

To show the main purpose of first-class citizen conception, imaging you have a complex data traversal algorithm. You want to select only a sub-set part of data members by some predicate and perform some action on each. The traversal algorithm should be universal, agnostic to both action and predicate. Here is how:

C#
void TraverseData(
         MyDataContainer data,
         Func<DataElement> predicate,
         Action<DataElement> dataProcessor) {
    //...
    DateElement element = //...
    if (predicate(element))
        dataProcessor(element);
    //...
} //TraverseData


[EDIT — fixed reference]
See also my other answer: What is the Extra Advantage of Delegate[^]

[EDIT]
For advanced use of delegates, see my new article on the topic:
Dynamic Method Dispatcher[^]

—SA
 
Share this answer
 
v5
Comments
Manfred Rudolf Bihy 9-Feb-11 14:49pm    
Good answer SA! 5+

One thing though don't call it "profile in the invocation list". There's a word for it in our terminology and that word is "signature".
Sergey Alexandrovich Kryukov 9-Feb-11 16:43pm    
Manfred, thanks a lot for giving me this help!
I modified: "method with the matching signature". Much better, right?
--SA
Manfred Rudolf Bihy 9-Feb-11 17:37pm    
I still think "in the invocation list" is superfluous. The method signature is both the order and type of the parameters, so "any method with the matching signature" suffices in my opinion. :)
Sergey Alexandrovich Kryukov 9-Feb-11 17:43pm    
This is not the structure I mean (English don't tolerate so long constructs, so this is my fault to abuse it in this case); not "matching in invocation list", but "accept matching methods"; I'll fix, thank you.
--SA
Manfred Rudolf Bihy 9-Feb-11 18:06pm    
Ok I think I now understood what you meant by invocation list. My bad! I wasn't thinking about the possiblilty of assigning more than one handler (with a matching signature). There's always somthing to learn ( or to remember as in my case) :-).
 
Share this answer
 
Comments
Abhinav S 9-Feb-11 8:48am    
Good links.
[no name] 9-Feb-11 22:56pm    
Thanks Abhinav.
Sergey Alexandrovich Kryukov 9-Feb-11 14:09pm    
Good, my 5, but see my answer: shorted explanation, more to the point.
--SA
[no name] 9-Feb-11 22:56pm    
Thanks SAKryukov
Couple of links to help you start understanding the advantages -
Delegates and their role in Events in C# .NET[^]
Understanding Delegates in C#[^]
 
Share this answer
 
Comments
Pravin Patil, Mumbai 9-Feb-11 6:27am    
Good links Abhinav....
Abhinav S 9-Feb-11 8:48am    
Thank you Pravin.
Sergey Alexandrovich Kryukov 9-Feb-11 14:08pm    
Good, my 5, but see my answer: explanation more to the point.
--SA
Abhinav S 9-Feb-11 23:32pm    
Saw it and already voted for it. :)
Sergey Alexandrovich Kryukov 10-Feb-11 13:08pm    
Thank you very much.

It's funny: more people claimed they voted, but actually only one voted.
(not a problem of course, happens all the time).
--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