mobile menu icon

Event bubbling in C#

Published by Carlos Blé on 08/04/2016

C#, .Net


How to propagate an event from a low level class to a top level one:

Events can only be raised from within the declaring type. Unfortunately they can’t be be passed in as arguments to methods. Only += and -= operators are allowed out of the declaring type. One way to stub out the event could be through inheritance:

But declaring the event as virtual and then overriding it, is very tricky: replacing the call to RaiseEvent to DoSomething, makes the test fail! Looks like events where not designed to be overridden.

A better approach would be:

Originally published in Carlos Blé's blog.

Volver a posts