Click here to Skip to main content
11,928,987 members (57,008 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0
Please tell me the answer for my question.
Posted 9-Feb-11 1:05am
Edited 9-Feb-11 1:12am
Member 8585141 3-Mar-12 1:39am
thank you .It is use full to me
Rate this: bad
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#[^]
Pravin Patil, Mumbai 9-Feb-11 6:27am
Good links Abhinav....
Abhinav S 9-Feb-11 8:48am
Thank you Pravin.
SAKryukov 9-Feb-11 14:08pm
Good, my 5, but see my answer: explanation more to the point.
Abhinav S 9-Feb-11 23:32pm
Saw it and already voted for it. :)
SAKryukov 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).
Abhinav S 10-Feb-11 13:32pm
Weird. I DID miss out voting for you. Fixed now.
Thanks for pointing this out.
SAKryukov 10-Feb-11 13:50pm
Thank you. Nothing weird: someone else forgot; As I say, not a problem.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Abhinav S 9-Feb-11 8:48am
Good links.
Ramalinga Koushik 9-Feb-11 22:56pm
Thanks Abhinav.
SAKryukov 9-Feb-11 14:09pm
Good, my 5, but see my answer: shorted explanation, more to the point.
Ramalinga Koushik 9-Feb-11 22:56pm
Thanks SAKryukov
Rate this: bad
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))
} //TraverseData

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

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

Manfred R. 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".
SAKryukov 9-Feb-11 16:43pm
Manfred, thanks a lot for giving me this help!
I modified: "method with the matching signature". Much better, right?
Manfred R. 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. :)
SAKryukov 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.
Manfred R. 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) :-).
SAKryukov 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.
Manfred R. Bihy 9-Feb-11 19:38pm
Ok so I did understand you right!
SAKryukov 9-Feb-11 20:33pm
SAKryukov 30-May-11 11:45am
Please see my new article on the topic (a link in my updated answer),

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 30 May 2011
Copyright © CodeProject, 1999-2015
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