C # - How to handle duplicates of multicastdelegate?

Because we register the PrintOne callback function twice , the following code prints the message β€œPrintOne” twice. Here are the questions

Question 1> Why, by default, the + = operator (i.e. Combination) does not check the handler of the repeating method?

Question 2> How to avoid this duplicate call in the RegisterCall method? I'm trying to find some method in MulticastDelegate / Delegate that can tell me that there is already one in the call list. But I did not find it. http://msdn.microsoft.com/en-us/library/system.multicastdelegate.aspx

thanks

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace app3 { class Car { public delegate void PrintMethod(string msg); public string Name { get; set; } private PrintMethod printMethods; public Car() { } public Car(string name) { Name = name; } public void PrintCar() { if (printMethods != null) { printMethods(this.ToString()); } else { Console.WriteLine("No Method will be called"); } } public override string ToString() { return string.Format("Car Name is {0}: ", Name); } public static void PrintOne(string msg) { Console.WriteLine("PrintOne"); } public static void PrintTwo(string msg) { Console.WriteLine("PrintTwo"); } public void RegisterCall(PrintMethod methodToCall) { printMethods += methodToCall; } } class Program { static void Main(string[] args) { Car mycar = new Car { Name = "BMW" }; mycar.RegisterCall(new Car.PrintMethod(Car.PrintOne)); // **will print for the first time** mycar.RegisterCall(new Car.PrintMethod(Car.PrintOne)); // **will print for the second time** mycar.PrintCar(); Console.ReadLine(); } } } 
+4
source share
2 answers
 public void RegisterCall(PrintMethod methodToCall) { printMethods -= methodToCall; printMethods += methodToCall; } 

This will ensure that it is deleted if it is present in the multicast delegate and then added to provide 1 instance.

Adding a handler is not interrupted if a delegate of the same handler is already present in the multicast delegate, because most duplicate times do not occur. There are also acceptable situations where calling the same method twice is what is required (for example, user aggregation on an object or collection).

If they decided that duplication would be eliminated, they would have to throw an exception when adding a handler. This is expensive in many ways, both at runtime and in all the ugly try-catch blocks that we would need to write.

+5
source

A typical design for registered callbacks in C # is to publish an event on your object. Thus, another class can add and, more importantly, remove event handlers. I don’t understand why you are using the RegisterCall method instead of using the built-in event registration functions in C #.

Typically, a module that adds an event handler to the event also removes this handler when the call is no longer needed. This is necessary for any event processing object whose life expectancy is shorter than the event generating object itself, since delegates store references to object instances and save them.

+1
source

All Articles