Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends,

I know the creation of delegate and events in CSharp. But i dont know where the delegate concept can be used in Projects.

Could anybody help me about the use of Delegate and events in Real time sinereo... If possible with an example.


Regards,
Sarath.M
Posted

 
Share this answer
 
Comments
BillWoodruff 20-Nov-11 11:52am    
Namaste, Thatraja-ji, I'd be curious what you think about my comment on the article you link to: http://www.codeproject.com/Messages/4082824/constructive-criticism-I-hope.aspx
Sergey Alexandrovich Kryukov 20-Nov-11 17:43pm    
Bill, your comment makes sense and the author already accepted your criticism.

I added my own point of criticism to this, please see http://www.codeproject.com/KB/cs/6ImportDelegatesAndEvents.aspx?msg=4082957#xx4082957xx.

What do you think?
--SA
BillWoodruff 20-Nov-11 23:13pm    
Hi SAK, yes I read your comments, and while I deliberately avoided mentioning the Invocation List and Multi-Cast Event-related aspects of Delegate both on this thread and my feedback on the article by Sri Shivaprasad ... in order to stay focused on only what a delegate is in the simplest way ...

Your comments were excellent, and far-superior to anything I could have written about those more advanced aspects.

I have sometimes wondered if .NET would have benefited from some simple one-level-of-indirection mechanism semantic without an invocation list and multi-cast machinery.

And then, 'Event' could have been 'Delegate + Invocation List + Multi-Cast ? Or, perhaps, there could be even a "junior" form of Event ('EventMinor' ?) that could possess and 'trigger' one and only one method body, or lambda, or whatever.

Of course, for this mind to fantasize about the design of a software framework and language from the mind of a genius like Anders Hejlsberg ... is absurd grandiosity* :)

best, Bill

* ... mmm .... is "absurd grandiosity" an oxymoron ?
Just Check out the following link this will help you to understand the use
of Events and Delegates
Use Of Events and Delegates.
 
Share this answer
 
I feel like I could think of a million examples! Delegates are very useful in a lot of situations. Sadly no one here really has the time to lay out a bunch of examples with explanations for you to learn from. So my advice is to check out the links in the other solutions and just keep reading and writing source code. Eventually the many uses of delegates will start to reveal themselves to you. Good luck! :)
 
Share this answer
 
I have a few suggestions on how you can go about "mastering" delegates and events.

Consider this delegate example:  
public delegate string Del_ReturnSumAsString(int x, int y);

1. start with focusing on what a "delegate" is:

  a. a "signature," or "blueprint," or "template" for a method

  b. that must specify a return Type (or "void"), and that must specify a list of parameters in the same way a method/function specifies them in .NET.

  ... in the example above you describe the signature of a method that will return an object of Type string, and that takes two objects of Type int as parameters ...

2. then focus on how you create an "instance" of a delegate:

  a. just like you declare instances of variables:  
public Del_ReturnSumAsString ReturnSumAsString;
  ...You have now declared 'ReturnSumAsString as a special type of instance: perhaps think of it as a "place-holder," or "container" for a method definition that exactly matches the delegate's signature.

3. now you can "insert" into the instance of the delegate a pointer to a method: so let's define a method, and insert it:
C#
public string TwoIntsToString(int x, int y)
{
    return (x + y).ToString();
}
So now we have the delegate, the instance of the delegate, and method declared ready-to-be-assigned to the instance of the delegate.

How do we assign our method to the instance of the delegate ? We have to do this in code that is executed at run-time. Let's use a Form's Load event as an example:
C#
private void Form1_Load(object sender, EventArgs e)
{
    ReturnSumAsString = TwoIntsToString;
}
And, now, we can invoke the instance of the delegate as if we had executed the function 'TwoIntsToString:
string result = ReturnSumAsString (3, 4);
At this point, I hope you are wondering why we have gone to so much trouble to create such an "indirect" way of calling the function 'TwoIntsToString.

We'll conclude this little journey by saying that this "indirection" we have created is: a "de-coupling" of the name of a method and its executable code.

But, that's just the beginning of the journey ! However, I guarantee you, that, if you understand what a delegate is, as presented here, you will have a foundation to build on, as you go on to consider how delegates relate to events, and their other uses ... or your money back :)

"rahi gulzar to phool khilenge" Kabir
 
Share this answer
 

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