Proposal: Add +==
and *=
operators for idempotent and weak event subscriptions in C#
#9348
Unanswered
rudolfstepan
asked this question in
Language Ideas
Replies: 1 comment 5 replies
-
Idempotent is coupled with implementation of event, not its API surface. It's not possible to use different operators by the callers to implement these. The For weakness, it is possible for the caller to construct a indirect weak closure, but the transformation is too much. It's also not so inefficient. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Event Operator Proposal for C#
This proposal introduces two new C# operators for event handling, aimed at improving safety, expressiveness, and developer ergonomics in event-driven code:
+==
— Idempotent event registrationAdds an event handler only if it hasn't already been registered.
*=
— Weak event registrationRegisters a handler without creating a strong reference, helping to prevent memory leaks in publisher/subscriber patterns.
✨ Motivation
C# has made great strides in improving language clarity and reducing boilerplate with operators like
??
,??=
, and?..
. However, event handling remains relatively low-level and prone to common runtime pitfalls:These proposed operators address those issues directly, while maintaining the elegance and intent-revealing nature of C#.
💡 Examples
These operators would significantly simplify codebases relying on dynamic subscriptions or MVVM/Reactive architectures.
📎 Full Proposal
The detailed rationale, design considerations, examples, and potential syntax alternatives are outlined here:
👉 https://github.com/rudolfstepan/event_operator_proposal
🔧 Implementation considerations
These operators could be:
WeakReference
🧩 Compatibility
A working example using legacy C#:
I’ve implemented the core idea of a weak event handler pattern in a production-ready form here:
🙏 Closing
This proposal aims to bring modern expressiveness and safety to one of C#'s most fundamental patterns.
Feedback and further discussion are very welcome!
Beta Was this translation helpful? Give feedback.
All reactions