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

I have a lot of code like below where I explicitly implement some of the events required by the 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 most of this is boilerplate. In Ruby, I could do something like this:

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

      

Is there a shorter way to remove this pattern in C #?


Update: I left out a very important part from my example: namely, the events that were implemented are part of the interface and I want to implement it explicitly. Sorry I didn't mention this before!

+2


a source to share


4 answers


Try the following:

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

      

This code uses C # field event syntax :

When you compile a field-like event, the compiler automatically creates a store to hold the delegate and create event accessories that add or remove event handlers to the delegate's field.

In C # 1, 2 and 3, this code will compile up to what you have above. In C # 4, you'll end up with functionally equivalent code that doesn't use explicit locks . In any case, you can use this shortcut without changing the consumers of that type.

Update: Unfortunately, the C # compiler doesn't allow you to use field-like events to explicitly implement an interface. If you try you get this compilation error:



An explicit interface implementation for an event must use the event access syntax

Update: It's too bad that implementing an explicit interface requires using the event access syntax. It would be great 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 longer than the existing field-like event syntax and is only applicable in cases where the interface element is explicitly implemented. Best I think it would be if the compiler just let us do this:

event EventHandler<EventArgs> IAppliance.DoorClosed;

      

+10


a source


I'm not sure what Lock

it does as I'm kind of new, but you can hook all controls or anything that triggers those events to just one event. In this case, just follow the statement if

to check who is sending it.



It would look a lot neat I would think, but it still takes some work I think.

0


a source


Write a code snippet for it and use a shortcut. Also when you use if, for, foreach {tab} {tab}.

0


a source


The code snippets suggested by gbogumil will save you some time. Alternatively, if there is a specific interface that you regularly implement that requires it, is it possible to create an abstract base class?

0


a source







All Articles