In C#, subscribing for the second time to the same event, does not overwrite the first subscribtion. Instead it ads a second subscribtion. A simple hack to prevent this is to always unsubscribe to the event first:
sprite.PointerPressed -= OnCardPointerPressed; // unsubscribe
sprite.PointerPressed += OnCardPointerPressed; // subscribe
If there was no subscription, the un-subscription is ignored. The code is "idempotent". Idempotent means that no matter how often we call this code, the result is always exactly a single subscription.
There are more examples:
flag = true;
flag = true;
flag = true;
No matter how often flag is set to true again, the result remains true.
Or in math:
abs(abs(-5))
It does not matter how often you nest the abs function, the result remains positive.
Why would you do it? Well, not because you like to do things twice but because you don't want to or cannot check if it is already done. For example, in the case of the C# event, it is not possible to check for suibscriptions due to encapsulaton. In this case, it is harmless to use "Idempotent" code like unsubscribe and then subscribe to the event.
Written by Loek van den Ouweland on Jan. 21, 2026.