Youtube video on Delegates
Click here for Video on Multicast Delegates
What is a delegate?
A delegate is a type safe function pointer. Using delegates you can pass methods as parameters. To pass a method as a parameter, to a delegate, the signature of the method must match the signature of the delegate. This is why, delegates are called type safe function pointers.
What is the main use of delegates in C#?
Delegates are mainly used to define call back methods.
What do you mean by chaining delegates?
Or
What is a multicast delegate?
The capability of calling multiple methods on a single event is called as chaining delegates. Let me give you an example to understand this further.
1. Create a new asp.net web application
2. Drag and drop a button control and leave the ID as Button1.
3. On the code behind file, add the code shown below.
When you click the Button now, both Method1 and Method2 will be executed. So, this capability of calling multiple methods on a single event is called as chaining delegates. In the example, we are using EventHandler delegate, to hook up Method1 and Method2 to the click event of the button control. Since, the EventHandler delegate is now pointing to multiple methods, it is also called as multicast delegate.
Will the following code compile?
Very Easy and Nice Concept. I like it.
ReplyDeleteExcellant
ReplyDeleteA delegate is a type safe function pointer.
ReplyDeleteI think this definition misses the following main thing about delegate which shows why it is type safe
Delegate is a class we are creating objects of the delegate and pass the method address to it. it works as a function pointer
A delegate is called as type safe because, the signature of the delegate must match the signature of the method the delegate is pointing to.
DeleteSimple understandble explanation... thanks
ReplyDeleteBeautiful Explanation in simple words..Thanks a ton
ReplyDeleteWhat is the difference between Delegate and an Event? Where do we use delegate in asp application?
ReplyDeletewhy we use delegate exactly??
ReplyDeletemethods when call give same output then why it is exactly used??
what is callback here??
Hello Venkat Sir,
ReplyDeleteI watch your C# video in youtude
Thank you sir you give lot of information to me
I have one question
please tell me the internal work of delegate(what happends when we call the method using delegate)
and Disadvantages of delegate
Thank you sir
what is callback here??
ReplyDeleteDifference between Delegate and an Event?
ReplyDelete