I’m looking to implement some extra logic when event handlers are added or removed to an event.
I’m aware that the default implementation in .net changed recently.
I’d like to keep my implementation as close to the default implementation as possible.
Can anyone point me to/provide something that shows how the compliler implements events?
See this series of blog posts.
In C# <4, it used simple delegate operations in
locks.In C# 4+, it uses a fancier lock-free algorithm by calling
Interlocked.CompareExchangein a loop; look at it in a decompiler for more detail.If you’re sure that your classes will never be used on multiple threads, you don’t need any of that; you can simply use unsynchronized delegate arithmetic.