136: Using the += and = Delegate Operators in C#.NET

Paint PDF417 in C#.NET 136: Using the += and = Delegate Operators
Listing 136: Using the += and = Delegate Operators
Making PDF 417 In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create PDF 417 image in .NET applications.
// Thermostat thermostat = new Thermostat(); Heater heater = new Heater(60); Cooler cooler = new Cooler(80); ThermostatTemperatureChangeHandler delegate1; ThermostatTemperatureChangeHandler delegate2; ThermostatTemperatureChangeHandler delegate3; // use Constructor syntax for C# 10 delegate1 = heaterOnTemperatureChanged; delegate2 = coolerOnTemperatureChanged;
Print Barcode In Visual C#.NET
Using Barcode maker for .NET framework Control to generate, create bar code image in .NET framework applications.
Coding the Observer Pattern with Multicast Delegates
Creating PDF417 In VS .NET
Using Barcode generation for ASP.NET Control to generate, create PDF417 image in ASP.NET applications.
ConsoleWriteLine("Invoke both delegates:"); delegate3 = delegate1; delegate3 += delegate2; delegate3(90); ConsoleWriteLine("Invoke only delegate2"); delegate3 -= delegate1; delegate3(30); //
Painting PDF-417 2d Barcode In VS .NET
Using Barcode maker for VS .NET Control to generate, create PDF417 image in VS .NET applications.
The results of Listing 136 appear in Output 131
Encode PDF 417 In VB.NET
Using Barcode printer for VS .NET Control to generate, create PDF 417 image in .NET framework applications.
OUTPUT 131:
Bar Code Maker In Visual C#.NET
Using Barcode creation for VS .NET Control to generate, create bar code image in VS .NET applications.
Invoke both delegates: Heater: Off Cooler: On Invoke only delegate2 Cooler: Off
Barcode Drawer In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create bar code image in .NET applications.
Furthermore, you can also use the + and operators to combine delegates, as Listing 137 shows
Create Code39 In Visual C#
Using Barcode generator for .NET framework Control to generate, create Code 39 Full ASCII image in VS .NET applications.
Listing 137: Using the + and - Delegate Operators
Code 128B Generator In Visual C#
Using Barcode encoder for Visual Studio .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications.
// Thermostat thermostat = new Thermostat(); Heater heater = new Heater(60); Cooler cooler = new Cooler(80); ThermostatTemperatureChangeHandler delegate1; ThermostatTemperatureChangeHandler delegate2; ThermostatTemperatureChangeHandler delegate3; // Note: Use new ThermostatTemperatureChangeHandler( // coolerOnTemperatureChanged) for C# 10 syntax delegate1 = heaterOnTemperatureChanged; delegate2 = coolerOnTemperatureChanged; ConsoleWriteLine("Combine delegates using + operator:"); delegate3 = delegate1 + delegate2; delegate3(60); ConsoleWriteLine("Uncombine delegates using - operator:"); delegate3 = delegate3 - delegate2; delegate3(60); //
Printing Data Matrix In C#
Using Barcode creation for .NET Control to generate, create ECC200 image in .NET framework applications.
13: Events
Painting Bar Code In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
Use of the assignment operator clears out all previous subscribers and allows you to replace them with new subscribers This is an unfortunate characteristic of a delegate It is simply too easy to mistakenly code an assignment when, in fact, the += operator is intended The solution, called events, appears in the Events section, later in this chapter It should be noted that both the + and operators and their assignment equivalents, += and -=, are implemented internally using the static methods SystemDelegateCombine() and SystemDelegateRemove() Both methods take two parameters of type delegate The first method, Combine(), joins the two parameters so that the first parameter points to the second within the list of delegates The second, Remove(), searches through the chain of delegates specified in the first parameter and then removes the delegate specified by the second parameter One interesting thing to note about the Combine() method is that either or both of the parameters can be null If one of them is null, Combine() returns the non-null parameter If both are null, Combine() returns null This explains why you can call thermostatOnTemperatureChange += heaterOnTemperatureChanged; and not throw an exception, even if the value of thermostatOnTemperatureChange is not yet assigned
Barcode Generator In Java
Using Barcode creation for Java Control to generate, create bar code image in Java applications.
Sequential Invocation The process of notifying both heater and cooler appears in Figure 131 Although you coded only a single call to OnTemperatureChange(), the call is broadcast to both subscribers so that from that one call, both cooler and heater are notified of the change in temperature If you added more subscribers, they too would be notified by OnTemperatureChange() Although a single call, OnTemperatureChange(), caused the notification of each subscriber, they are still called sequentially, not simultaneously, because a single delegate can point to another delegate that can, in turn, point to additional delegates
Data Matrix ECC200 Generation In Java
Using Barcode generation for Java Control to generate, create Data Matrix ECC200 image in Java applications.
_actor : actor 1 : Main() : Console 2 : Thermostat() Thermostat : Thermostat 3 : Heater(temperature) _heater : Heater 4 : Cooler(temperature) OnTemperatureChange delegate TemperatureChangeHandler 5 : operator+= _cooler : Cooler
Barcode Encoder In Visual Studio .NET
Using Barcode drawer for VS .NET Control to generate, create barcode image in .NET framework applications.
6 : operator+= 7 : Write("Enter temperature:") 7 : ReadLine() 9 : CurrentTemperature(value) 10 : OnTemperatureChange(value) 11 : OnTemperatureChanged(newValue) 12 : WriteLine("Heater off")
Bar Code Drawer In Java
Using Barcode printer for Java Control to generate, create bar code image in Java applications.
OnTemperatureChanged( ) 13 : OnTemperatureChanged(newTemperature ) 14 : WriteLine("Cooler on")
UPC - 13 Recognizer In VS .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
OnTemperatureChanged( ) OnTemperatureChange( ) CurrentTemperature( )
Data Matrix 2d Barcode Scanner In .NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
Figure 131: Delegate Invocation Sequence Diagram
Encoding Code 128C In VB.NET
Using Barcode generator for VS .NET Control to generate, create Code 128A image in .NET applications.
13: Events
Generating Barcode In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
ADVANCED TOPIC Multicast Delegate Internals To understand how events work, you need to revisit the first examination of the SystemDelegate type internals Recall that the delegate keyword is an alias for a type derived from SystemMulticastDelegate In turn, SystemMulticastDelegate is derived from SystemDelegate, which, for its part, comprises an object reference and a method pointer (of type SystemReflectionMethodInfo) When you create a delegate, the compiler automatically employs the SystemMulticastDelegate type rather than the SystemDelegate type The MulticastDelegate class includes an object reference and method pointer, just like its Delegate base class, but it also contains a reference to another SystemMulticastDelegate object When you add a method to a multicast delegate, the MulticastDelegate class creates a new instance of the delegate type, stores the object reference and the method pointer for the added method into the new
UCC - 12 Recognizer In Visual Studio .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in Visual Studio .NET applications.