Impact of Gen-AI on IT Jobs - Growth Mindset Show
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
2
Reply
What is a Multicast delegate?
Miky alton
13y
2.8k
0
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
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 .
Nikesh Patil
12y
0
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(); }
jitendra kumar
13y
0
Define Interface & What is the diff. between abstract & interface?
What problem does Delegate Solve?
Message