Studying delegates in the .NET framework

OData support
Dr. Asztalos Márk
Department of Automation and Applied Informatics

Programming in a high-level language is an efficient way to develop enterprise applications, as it hides the low-level details of the computer from the developer. They deal with abstract concepts, such as variables, objects, arrays and locks. This paper consists of the investigation of the internal operation of the .NET Framework.

The .NET framework realises event handling using the publisher-subscriber pattern, with delegates. Event handling is often used during software development, as it is integral part of every enterprise application and user interface. Using event handling without caution can lead to errors which are difficult to detect, for example memory leaks, if the developer didn’t handle the objects’ lifecycle carefully, and the object is left subscribed to an event. This kind of mistake is not hard to make as the lifecycle of an object in a managed environment is hard to follow. The probability of making such mistakes and the amount of the invested work could be significantly decreased by a mechanism, which automatically unsubscribes objects from events, when they are not used anymore. Unfortunately such a mechanism doesn’t really exist, more specifically existing solutions work under specific circumstances only.

This thesis consists of the development of a library, which uses weak references to prevent memory leaks, introduces the concept of weak delegates, and handles automatic unsubscribtion as well. This solution doesn’t need to prepare the publisher for weak event handling, and operates on every single type of delegates generally – unlike existing methods.

During the designing process I examine the behaviour of the delegates, and some built-in functions which operate on delegates. I make an implementation featuring the dynamic framework, genericty, and runtime code emitting one by one. Finally I compare all the implementations using tests and measurements, and present the usage of the best solution in an example application.

My goals are to solve the problem of weak event handling in an efficient way, and to make a library which I would personally find useful in my future development work.


Please sign in to download the files of this thesis.