Decompiled assembly - unusual code

核能气质少年 提交于 2019-12-02 02:44:41

This is a new event handler code generated by compiler. It was introduced in C# 4 (C# 3 version was different)

Interlocked.CompareExchange compares first argument with third, and if they are equal, replaces first argument with second. This is a thread-safe operation. Loop is used for a case when after assigning a variable eventHandler2 and before check, another thread changes this delegate. In this case, Interlocked.CompareExchange does not perform exchange, loop condition does not evaluate to true and next attempt is made.

C# 3 generated simple code in event handlers:

add { lock(this) { changed = changed + value; } }

Which had lower performance and could introduce deadlocks.

There is a great series of articles on this subject:

Events get a little overhaul in C# 4

Events get a little overhaul in C# 4, Part II

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!