In .NET, if I’m implementing an interface that contains an event, but that event makes no sense for my object (say it’s a change event and I’m writing an immutable object), then I can just provide empty bodies for add and remove — a null implementation. This avoids allocating storage for a delegate field I’ll never use, and also avoids the “event is never used” compiler warning, so it’s a win all around.
public event EventHandler Changed {
add {}
remove {}
}
When I try the same thing in a WinRT class (descends from FrameworkElement), I get a compiler error on the add accessor: “not all code paths return a value”.
How do I return a value from an add accessor? What’s it supposed to return?
Update: Apparently this problem only applies to WinRT events (e.g., if you’re implementing a WinRT interface that contains an event). If you’re writing a plain old CLR event, the above syntax works.
So based on the link Anders posted, it looks like the
addaccessor needs to return anEventRegistrationToken.EventRegistrationTokenis just a struct, apparently with no fields, and no constructors beyond the default, so it looks like I could harmlesslynewone up — especially since the only code that will consume it is myremove, which doesn’t care.So the WinRT equivalent of the null event appears to be: