Miky alton
What is a Multicast delegate?
By Miky alton in C# on May 13 2012
  • Nikesh Patil
    Oct, 2013 25

    It is a delegate which holds the reference of more than one method i.e Multicast delegate can point to multiple function and methods. If we want to call multiple methods using single delegate all the methods should have the same IO Parameters.In order to add multiple methods and function we need to use ‘+=' symbols .

    • 0
  • jitendra kumar
    May, 2012 13

    Any delegate that has a void return type, is a multicast delegate. A multicast delegate can be assigned and invoke multiple methods. You may have seen hints of this in your code. For example, if you are doing ASP.NET development and have ever looked at a page's InitializeComponent() method, you may have noticed a statement like this: this.SearchButton.Click += new System.EventHandler(this.SearchButton_Click); Notice the += in that statement. That is a clue. You could add another event handler if you wanted, and it would get called as well. For example, let's say you wanted to also call a method named NotifyStatistics every time the Search button was clicked. Then your code could be: this.SearchButton.Click += new System.EventHandler(this.SearchButton_Click); this.SearchButton.Click += new System.EventHandler(this.NotifyStatistics); Every time the user clicks the Search button, first SearchButton_Click() is called, followed by NotifyStatistics(). The event handlers are called in the order they are added. Likewise, using -=, an event handler can be removed. The Code In my announcement editor module, I have added the following public method: public void RegisterForSaveNotification(System.EventHandler eventHandler) { this.SaveAnnouncementButton.Click += eventHandler; } In my announcement viewer module, I have added the following public method: public void RefreshAnnouncement() { // My code to load the announcement from the database goes here. announcementContent.InnerHtml = Announcements.GetAnnouncement(EntityType.Text, EntityId.Text, Int32.Parse(Sequence.Text)); } And, to tie it all together, here is the code I have added to the page that they are both on: In Page_Load, I have added the following call to RegisterForSaveNotification(): RegisterForSaveNotification(); if (!Page.IsPostBack) { ... } I show the call in context of the check for postback because the event handler must be registered for gets and posts. I would prefer to make the call in InitializeComponent(), but Visual Studio keeps deleting it when I put it there. private void RegisterForSaveNotification() { // Register for save notification so we can have the announcement viewer update. AnnouncementEditorControl1.RegisterForSaveNotification(new System.EventHandler(this.SaveAnnouncementNotification)); } private void SaveAnnouncementNotification(object sender, System.EventArgs e) { // We have been notified that the announcement has been updated, so have the viewer refresh. AnnouncementViewControl1.RefreshAnnouncement(); }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS