Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Please tell me the answer for my question.
Posted 9-Feb-11 1:05am
Edited 9-Feb-11 1:12am
CPallini326.2K
v3
Comments
Member 8585141 at 3-Mar-12 1:39am
   
thank you .It is use full to me
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Couple of links to help you start understanding the advantages -
Delegates and their role in Events in C# .NET[^]
Understanding Delegates in C#[^]
  Permalink  
Comments
Pravin Patil, Mumbai at 9-Feb-11 6:27am
   
Good links Abhinav....
Abhinav S at 9-Feb-11 8:48am
   
Thank you Pravin.
SAKryukov at 9-Feb-11 14:08pm
   
Good, my 5, but see my answer: explanation more to the point.
--SA
Abhinav S at 9-Feb-11 23:32pm
   
Saw it and already voted for it. :)
SAKryukov at 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
Abhinav S at 10-Feb-11 13:32pm
   
Weird. I DID miss out voting for you. Fixed now.
Thanks for pointing this out.
SAKryukov at 10-Feb-11 13:50pm
   
Thank you. Nothing weird: someone else forgot; As I say, not a problem.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Abhinav S at 9-Feb-11 8:48am
   
Good links.
Ramalinga Koushik at 9-Feb-11 22:56pm
   
Thanks Abhinav.
SAKryukov at 9-Feb-11 14:09pm
   
Good, my 5, but see my answer: shorted explanation, more to the point.
--SA
Ramalinga Koushik at 9-Feb-11 22:56pm
   
Thanks SAKryukov
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
    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
  Permalink  
v5
Comments
Manfred R. Bihy at 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".
SAKryukov at 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 R. Bihy at 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. :)
SAKryukov at 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 R. Bihy at 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) :-).
SAKryukov at 9-Feb-11 19:34pm
   
No, no, thank you for your notes, because original text (very fast typed of course) contained ambiguity, due to structural abuse I made.
Any delegate instance (any, I mean it) contains invocation list, every "+=" adds one more item, every "=" clears them all set only one -- not everyone knows that, it is more known about events).
My "no" does not apply to "There's always something to learn", there is, indeed, a lot.
--SA
Manfred R. Bihy at 9-Feb-11 19:38pm
   
Ok so I did understand you right!
SAKryukov at 9-Feb-11 20:33pm
   
:-)
SAKryukov at 30-May-11 11:45am
   
Please see my new article on the topic (a link in my updated answer),
--SA

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

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100