Search code examples
c#refactoringeventsboilerplate

Is there a better way to write this repetitive event-declaration code in C# when implementing an interface explicitly?


I have a lot of code like the following, where I explicitly implement some events required by an interface.

public class IMicrowaveNotifier {
  event EventHandler<EventArgs> DoorClosed;
  event EventHandler<EventArgs> LightbulbOn;
  // ...
}

public class Microwave : IMicrowaveNotifier {
  private EventHandler<EventArgs> _doorClosed;
  event EventHandler<EventArgs> IMicrowaveNotifier.DoorClosed {
    add { lock (this) _doorClosed += value; }
    remove { lock (this) _doorClosed -= value; }
  }

  private EventHandler<EventArgs> _lightbulbOn;
  event EventHandler<EventArgs> IMicrowaveNotifier.LightbulbOn {
    add { lock (this) _lightbulbOn += value; }
    remove { lock (this) _lightbulbOn -= value; }
  }

  // ...
}

You can see that much of this is boilerplate. In Ruby I'd be able to do something like this:

class Microwave
  has_events :door_closed, :lightbulb_on, ...
end

Is there a similar shorter way of removing this boilerplate in C#?


Update: I left a very important part out of my example: namely, the events getting implemented are part of an interface, and I want to implement it explicitly. Sorry for not mentioning this earlier!


Solution

  • Try this:

    public class Microwave {
        public event EventHandler<EventArgs> DoorClosed;
        public event EventHandler<EventArgs> LightbulbOn;
    }
    

    This code leverages C#'s field-like event syntax:

    When compiling a field-like event, the compiler automatically creates storage to hold the delegate, and creates accessors for the event that add or remove event handlers to the delegate field.

    In C# 1, 2, and 3 this code will compile down to just what you have above. In C# 4 you will get functionally equivalent code that doesn't use explicit locks. Either way you can use this shortcut without changing the consumers of this type.

    Update: Unfortunately, the C# compiler does not allow you to use field-like events for explicit interface implementation. If you try you will get this compilation error:

    An explicit interface implementation of an event must use event accessor syntax

    Update: It's too bad that an explicit interface implementation requires the use of event accessor syntax. It would be kind of cool if C# added the ability to create automatically implemented field-like events like this:

    event EventHandler<EventArgs> IAppliance.DoorClosed { add; remove; }
    

    But this syntax is already wordier than the existing field-like event syntax and would only be applicable in cases where an interface member was being explicitly implemented. The best thing I think would be if the compiler would simply allow us to do this:

    event EventHandler<EventArgs> IAppliance.DoorClosed;