Felix K.
Felix K.

Reputation: 6281

Event add and remove implementation details

I'm developing on a application which has a lot of performance critial code. When looking at my code i noticed that i also have a lot of += and -=on events with many invokations, so i ask myself ( and now you ) how += and -=are implemented and how fast it is when have a lot of invokations.

Upvotes: 0

Views: 81

Answers (1)

JSBձոգչ
JSBձոգչ

Reputation: 41378

Underlyingly, an event is a list of event delegates. Adding a new event is similar in performance to calling List<T>.Add() (which is generally O(1)), while removing a delegate is equivalent to List<T>.Remove(), which is O(n) where n is the number of delegates in the list.

(This dosen't necessarily mean that delegates are actually implemented with a List<T> under the covers, but the performance characteristics are the same.)

Upvotes: 2

Related Questions