C# Interview Questions on Delegates


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?

No, the code does not compile. For the code to compile, the signature of Method1 should match the signature of SampleDelegate.

11 comments:

  1. Very Easy and Nice Concept. I like it.

    ReplyDelete
  2. A delegate is a type safe function pointer.

    I 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

    ReplyDelete
    Replies
    1. 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.

      Delete
  3. Simple understandble explanation... thanks

    ReplyDelete
  4. Beautiful Explanation in simple words..Thanks a ton

    ReplyDelete
  5. What is the difference between Delegate and an Event? Where do we use delegate in asp application?

    ReplyDelete
  6. why we use delegate exactly??
    methods when call give same output then why it is exactly used??
    what is callback here??

    ReplyDelete
  7. Hello Venkat Sir,
    I 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

    ReplyDelete
  8. what is callback here??

    ReplyDelete
  9. Difference between Delegate and an Event?

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us