Difference between Methods and Events in C# [Resolved]

Posted by Kasani007 under C# on 10/9/2014 | Points: 10 | Views : 481 | Status : [Member] | Replies : 2
Difference between Methods and Events in C#




Responses

Posted by: Goud.Kv on: 10/10/2014 [Member] [MVP] Gold | Points: 50

Up
0
Down

Resolved
Hi Chaitanya,

Methods are the code blocks (Enclosed with curly braces { and }) which contains series of statements that are being executed serially.

Events are used to provide notifications to the clients about the things such as meetings, reminders and birthdays etc.

Thanks.....

Thanks & Regards,
Krishna

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 10/10/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

C# Events are a specific form of delegates. If you have programmed in other languages, like C++, you could compare a delegate to a function ("method") pointer - it points to some code in memory. When you call the pointer as a method, you actually call the method at the address the pointer points to.

This is necessary to provide decoupling between the caller and the callee - so you don't have to have all methods ready when you publish code that calls them (which, wouldn't be possible - the Forms controls developers can't possibly know the code that needs to be called when a Button is pressed). You call the pointer, and the other developer sets it to an appropriate memory address later.

P.S. delegates and Events however, have other advantages over plain function pointers - you can be sure that it will point to a good-looking method, taking the correct number and type of arguments and returning the correct type.

Thank you,
Govind

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response