I understand what delegates are but unable to understand real advantage of using Delegates?
Can someone suggest with example how it's beneficial?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Jul 8, 2014, 4:45 AM
This is important if the method needs to 'call back' another method directly or indirectly.
If you look at the LINQ extension methods, you'll find that most of them take delegates as parameters and, since lambda expressions can be automatically converted to compatible delegates, this means that you can pass code 'inline' rather than passing an actual method pointer.
Also delegates are the underlying mechanism for implementing events in .NET and, since you can call several method at once with a single 'multicast' delegate, this means that you can make multiple simultaneous notifications when an event occurs which can be handled accordingly.