I'm subscribing to an event generated by a class that might be null as follows:
if (eventGeneratingClass != null)
eventGeneratingClass.myEvent += myHandler;
In the event generating class a construct like
myEvent?.Invoke(this, new EventArgs());
can be used. I was wondering if a similar construct could be used when (de)subscribing to an event, e.g.:
eventGeneratingClass?.myEvent += myHandler;
eventGeneratingClass?.myEvent -= myHandler;
I've found both Is there a shorthand for addition assignment operator for nullables that sets the value if null? and How to call custom operator with Reflection
which lead me to: Operator Overloading Usage Guidelines
Which lead me to trying
eventGeneratingClass?.myEvent.op_AdditionAssignment(myHandler);
eventGeneratingClass?.myEvent.op_SubtractionAssignment(myHandler);
but it doesn't seem that an event has an op_AdditionAssignment
memberfunction or at least provides me with the error
the event 'EventGeneratingClass.myEvent' can only appear on the left hand side of += or -=...
Which is where I got stuck...