We ran into a problem yesterday that took a while to figure out.  It was one of those programming issues where you know there are about 3 lines of code you need to write, but figuring out which 3 lines takes a while.

We had two interfaces with events with the same name and the same object needed to implement both.  We knew this wasn't a problem for methods so we suspected that it wasn't a problem for events either.  We just had to figure out the C# syntax.

Our first attempt went something like this:

class MyClass : NS1.IFoo, NS2.IBar
{
    event NS1.MyEvent NS1.IFoo.EventName;
    event NS2.MyEvent NS2.IBar.EventName;
} 

That produced compiler error CS0071: An explicit interface implementation of an event must use property syntax.  A quick search on MSDN got us some sample code. Too bad the sample code was incorrect.  I've filed a bug with MSDN.

Based on the incorrect sample code, attempt number two was roughly:

class MyClass : NS1.IFoo, NS2.IBar
{
    event NS1.MyEvent NS1.IFoo.EventName
    {
        get { return null; }
        set { }
    }
} 

That produced compiler error CS1055: An add or remove accessor expected.  This time MSDN was more helpful and gave us the correct syntax, although the code still didn't actually work.

class MyClass : NS1.IFoo, NS2.IBar
{
    event NS1.MyEvent NS1.IFoo.EventName
    {
        add { EventName += value; }
        remove { EventName -= value; }
    }
    void FireEventName()
    {
        if (EventName != null) { EventName(); }
    }
} 

As we typed the code, it seemed like it wasn't going to work.  The add accessor calls the add accessor which calls the add accessor, etc.  Interestingly, the compiler didn't castigate us for that but it did complain about our attempt to fire the event with CS0079: The event can only appear on the left hand side of += or -=.

This time MSDN's sample code gave use the full picture and we used a couple of private events.  The final code went something like this.

class MyClass : NS1.IFoo, NS2.IBar
{
    private event NS1.MyEvent InternalEvent1;
    private event NS2.MyEvent InternalEvent2;
    public event NS1.MyEvent NS1.IFoo.EventName
    {
        add { InternalEvent1 += value; }
        remove { InternalEvent1 -= value; }
    }
    public event NS2.MyEvent NS2.IBar.EventName
    {
        add { InternalEvent2 += value; }
        remove { InternalEvent2 -= value; }
    }
    void FireEvents()
    {
        if (InternalEvent1 != null) { InternalEvent1(); }
        if (InternalEvent2 != null) { InternalEvent2(); }
    }
} 

You probably already knew all of this.  But my inept Google search didn't really find much (but I can never find anything) and Inside C# doesn't talk about this.  Maybe this will be helpful, although I still have my doubts that anyone will ever read this blog...

The process did confirm two beliefs.  First, MSDN contains all the information that you need if you can just find it.  Second, the makers of C# are way smart and have thought of everything. Well, probably not everything or they wouldn't be working on Whidbey.