I have the following property, which will sometimes be null during the cleanup and closing of my application.
class Provider
{
public EventsContainer Events => condition ? m_eventsContainer : null;
...
}
I want to unsubscribe from an event delegate, only if the object exists.
However, attempting to do
void OnDisable()
{
m_provider.Events?.OnEventInvoked -= Funtion;
}
Raises this error
Compiler Error CS0079: The event ‘OnEventInvoked’ can only appear on the left hand side of += or -=
To me, it seems like it already is “on the left hand side of += or -=”.
Currently my workaround is to create a local copy of the object.
if (m_provider.Events is { } events)
events.OnEventInvoked -= Funtion;
But this is of course not ideal.
My question is, why am I getting this error (the error message seems incorrect to me) and is there a better way to work around or avoid this issue.