O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using .NET Events

Events are just delegates in disguise but provide additional levels of protection against the attacks we’ve seen previously. Syntactically, very little has to change:

class CountDown {    private uint _seconds;    public CountDown(uint seconds) {        _seconds = seconds;    }    public event Action<uint> Tick;    public event Action       Finished;    public void Start() {        ...    }}

See the difference? Instead of Tick and Finished being declared as properties with a delegate type, we now use the event keyword instead and drop the get and set property accessors. (You’ll see later how events have optional accessors called add and remove.)

At the use site, we can no longer write the following: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required