-
Notifications
You must be signed in to change notification settings - Fork 446
Naked delegates should have event modifier keyword #3137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Other than:
Everything else is either an However, I do appreciate the insights you shared and I will definitely keep that pattern in mind. Other than the 4 above listed, is this suggestion really more focused on making |
I think mainly just this one is the most concerning since anyone can stomp delegate subscriptions with a simple typo. |
Why is that? Are you expecting people to set their own delegate for a callback? Better to use a |
That is a good point and it will be something worth discussing as a potential improvement.
Because namespace System
{
public delegate TResult Func<out TResult>();
} And
Where as the reason behind not making it an event is the implicit usage pattern of an Action vs an event Action where the former implies that you really should just use a single callback (i.e. direct assignment) as opposed to have several subscribers. Where just typing this makes me think more about trying to stick with this pattern and updating NetworkVariable.OnValueChanged to be |
com.unity.netcode.gameobjects/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariable.cs
Line 23 in dcaeef9
Delegate events like the one shown above should have
event
keyword modifiers.A "naked" delegate refers to a delegate that is declared without the
event
keyword. This means the delegate is exposed directly, allowing external code to not only subscribe and unsubscribe to it but also to reassign or invoke it directly. This can lead to unintended side effects and potential bugs.For example, in the provided code snippet, the delegates are correctly declared as events:
If these were declared as naked delegates, they would look like this:
Issues with naked delegates:
Reassignment: External code can reassign the delegate, potentially removing all existing subscribers.
Invocation: It breaks encapsulation, making it harder to control how and when the delegate is invoked.
Dangling Pointers: Events in C# are multicast delegates which can have multiple and anonymous methods subscribe to the same event. Using the event keyword will help properly manage those subscriptions and unsubscriptions. This way if someone forgets to unsubscribe, the dangling pointer will get properly cleaned up by garbage collector.
TR;DL:
I've seen this throughout the whole codebase and highly encourage the use of the
event
keyword for all usages of delegate callbacks.Using the
event
keyword ensures that the delegate can only be invoked from within the class that declares it, maintaining proper encapsulation and control over the event's lifecycle and avoiding memory pressure from forgetting to unassign the delegates after use.The text was updated successfully, but these errors were encountered: