O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Cloning delegates

delegates implement the ICloneable interface so that a delegate can be copied. The copy will be a deep copy so that each copy can exist independently, and changes to one will not affect changes in the other. Listing 14.19 shows a simple illustration. The full source for this code is in the DelegateClone directory.

Listing 14.19. Cloning delegates
 DelegateCallback ad = new DelegateCallback(ACallback); DelegateCallback bd = new DelegateCallback(BCallback); DelegateCallback cd = new DelegateCallback(CCallback); DelegateCallback dd = ad; dd += bd; dd += cd; DelegateCallback ed = dd; Console.WriteLine("Original list"); Delegate [] da = dd.GetInvocationList(); foreach(Delegate d in da) { Console.WriteLine("{0} {1} ", d.Target, d.Method); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required