Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在开发具有大量性能关键代码的应用程序。在查看我的代码时,我注意到我也有很多+=和-=关于许多调用的事件,所以我问自己(现在是你)如何实现+=和-=实现,以及当有很多调用时它有多快。
+=
-=
从根本上讲,事件是事件委托的列表。添加新事件在性能上与调用类似List<T>.Add()(通常为 O(1)),而删除委托相当于List<T>.Remove()O(n),其中n是列表中委托的数量。
List<T>.Add()
List<T>.Remove()
(这并不一定意味着代表实际上是在幕后实现的List<T>,但性能特征是相同的。)
List<T>