HI, I want to know, any scenario or example where i can use delegate to call a method at runtime. Till now i did not feel any need to call a method runtime. so please elaborate this concept. Thanks in Advance.
Hi, below are the situation where u should use Delegates. 1. If you want to create a event and want to run different methods, on different scenarios. 2. If you want to run multiple methods in response to a single event. 3. Callback mechanism - If you are developing a component(asynchronous) which needs to send an alert to the caller after it s completion, then you need to know the caller s alert function (which is not feasible in real time). So to overcome that, caller should send some function pointer (i.e. delegate) to the component, to call it back after the task. You will always use a delegate to respond to an event. An event is a trigger mechanism, but it s the delegate that satisfies it. Even when you use the += notation, you are implicitly wrapping a member as a delegate and passing it to the event. There are plenty of times when delegates make sense. In command binding, for example, the ICommand interface has an "execute" and a "canexecute" method. You could do a separate implementation for every scenario, or you can use the DelegateCommand pattern that simply allows you to inject functionality to execute and determine if you "can execute." When simplifying services, you most likely do NOT want to expose the asynchronous call structure to the rest of your application because of the overhead. A simple way to abstract this is to pass in a callback delegate to your service layer, wait for the service call to complete, and call back. If you are programming in WPF or Silverlight, there are many times you have work done on a separate (background) thread that must update the UI. In that scenario, the values must be marshalled to the dispatcher or main UI thread. This is done by passing the action as a delegate to the thread.