epitka
epitka

Reputation: 17637

Am I missing something or is this not right way to declare event?

I came across this piece of code. It works, but is it not against the .net event declaration guidelines?

public event Action SessionTimeout;

Upvotes: 2

Views: 79

Answers (3)

Nicole Calinoiu
Nicole Calinoiu

Reputation: 20992

The convention is that a .NET event uses a delegate that takes two parameters: the sender and the event arguments. However, this is an API convention, not a requirement. You may declare an event of any delegate type.

Upvotes: 1

Adam Rackis
Adam Rackis

Reputation: 83358

I would change it to:

public event Action SessionTimeout = delegate {};

So you don't have to check for null before raising it. I assume the potential extra CPU cycle this will cause won't affect your performance :)

Upvotes: 1

Femaref
Femaref

Reputation: 61437

While it is against standard C# practice, it is not an invalid event decleration.

Upvotes: 3

Related Questions