Skip to content
Discussion options

You must be logged in to vote

The purpose of a raise accessor is to give external code the ability to raise the event, which is not considered good practice. Any custom code required to raise the event internal to the publisher can be just as easily managed by a custom method within the class and has never required an accessor or additional language features. The idiomatic C# approach has always been:

private readonly List<Action> MyEventInvocationList = new();
public event Action MyEvent
{
    add => MyEventInvocationList.Add(value);
    remove => MyEventInvocationList.Remove(value);
}

protected void OnMyEvent()
{
    MyEventInvocationList.ForEach(action => action());
}

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@Reinms
Comment options

Answer selected by Reinms
Comment options

You must be logged in to vote
2 replies
@Reinms
Comment options

@huoyaoyuan
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants