Skip to main content

Posts

Showing posts from October, 2014

Generalized Multicast Delegates in Pure C#

.NET's delegates are a powerful and convenient abstraction available since .NET 1.1. They encapsulate a method pointer and the object the method belongs to in a single callable "function object". Multicast delegates are an extension of plain delegates, in that they encompass multiple single delegates. Invoking a multicast delegate invokes every encapsulated delegate, in the order they were added. Multicast delegates are key to event handling patterns that were a core part of the CLR nearly since its inception.If you're curious about virtual machines or CLR internals, you've perhaps wondered how multicast delegates actually work. These are the key properties of multicast delegates:They encapsulate a list of delegates of the same delegate type.Adding a delegate returns a new multicast delegate containing the new addition at the end of the list (the original is unchanged).Removing a delegate returns a new multicast delegate without the specified delegate (the origin…