36

Small question about C# language design :))

If I had an interface like this:

interface IFoo {
  int Value { get; set; }
}

It's possible to explicitly implement such interface using C# 3.0 auto-implemented properties:

sealed class Foo : IFoo {
  int IFoo.Value { get; set; }
}

But if I had an event in the interface:

interface IFoo {
  event EventHandler Event;
}

And trying to explicitly implement it using field-like event:

sealed class Foo : IFoo {
  event EventHandler IFoo.Event;
}

I will get the following compiler error:

error CS0071: An explicit interface implementation of an event must use event accessor syntax

I think that field-like events is the some kind of dualism for auto-implemented properties.

So my question is: what is the design reason for such restriction done?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
controlflow
  • 6,667
  • 1
  • 31
  • 55
  • 1
    I am sure Andreas Huber's explanation is correct. You can work around the problem by having another private event that you call from the explicit implementation. That way you will get all the automatic features (thread-safety stuff etc.) from field-like event functionality. Example: `sealed class Foo : IFoo { event EventHandler privateEventFieldLike; event EventHandler IFoo.Event { add { privateEventFieldLike += value; } remove { privateEventFieldLike -= value; } } }` – Jeppe Stig Nielsen Mar 31 '17 at 09:39

4 Answers4

32

Interesting question. I did some poking around the language notes archive and I discovered that this decision was made on the 13th of October, 1999, but the notes do not give a justification for the decision.

Off the top of my head I don't see any theoretical or practical reason why we could not have field-like explicitly implemented events. Nor do I see any reason why we particularly need to. This may have to remain one of the mysteries of the unknown.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 9
    Now this is why I still visit SO sometimes... Where else would people take "I don't know" for a good answer. – ima Feb 16 '10 at 08:50
  • 4
    It seems the reason might be that you could never raise such an event, unless explicitly implemented events had different visibility rules than explicitly implemented methods, as explained in my answer... –  Feb 07 '12 at 15:32
  • 1
    I think Andreas Huber's answer is reasonable. – Jeffrey Zhao May 23 '12 at 03:10
31

I guess it might have to do with the fact that you can't call an explicit interface implementation from other members of the class:

public interface I
{
    void DoIt();
}

public class C : I
{
    public C()
    {
        DoIt(); // error CS0103: The name 'DoIt' does not exist in the current context
    }

    void I.DoIt() { }
}

Note that you can call the method by upcasting to the interface first:((I)this).DoIt();. A bit ugly but it works.

If events could be explicitly implemented as ControlFlow (the OP) suggested, then how would you actually raise them? Consider:

public interface I
{
    event EventHandler SomethingHappened;
}

public class C : I
{
    public void OnSomethingHappened()
    {
        // Same problem as above
        SomethingHappened(this, EventArgs.Empty);
    }

    event EventHandler I.SomethingHappened;
}

Here you cannot even raise the event by upcasting to the interface first, because events can only be raised from within the implementing class. It therefore seems to make perfect sense to require accessor syntax for explicitly implemented events.

  • Good answer. Seems plausible to me. Thanks. – Drew Noakes May 02 '12 at 22:53
  • 1
    I agree. A field-like event consists of three things: The `add` accessor, the `remove` accessor, and private access to the underlying generated field of delegate type. That delegate field is used directly when you use `MyEvent != null` or `local = MyEvent` or `MyEvent(sender, args)` or so on. However, only the `add` and `remove` accessors are part of the interface "contract". – Jeppe Stig Nielsen Mar 31 '17 at 11:27
  • In C# 6.0 (2015) it is possible to make an auto-property which is `get`-only. It is then possible to assign to it from a constructor of the same class. That really assigns to the underlying field. For example `sealed class Foo { public int Value { get; } public Foo() { Value = 42; } }`. If you try to do this with explicit interface implementation, the C# compiler _will_ allow it, but you can no longer assign in a constructor (backing field is not part of the `get`-only interface "contract"). `sealed class Foo : IFoo { int IFoo.Value { get; } public Foo() { } }` – Jeppe Stig Nielsen Mar 31 '17 at 11:28
23

When explicitly implementing an event that was declared in an interface, you must use manually provide the add and remove event accessors that are typically provided by the compiler. The accessor code can connect the interface event to another event in your class or to its own delegate type.

For example, this will trigger error CS0071:

public delegate void MyEvent(object sender);

interface ITest
{
    event MyEvent Clicked;
}

class Test : Itest
{
    event MyEvent ITest.Clicked;  // CS0071
    public static void Main() { }
}

The correct way would be:

public delegate void MyEvent(object sender);

interface ITest
{
    event MyEvent Clicked;
}

class Test : Itest
{
    private MyEvent clicked;

    event MyEvent Itest.Clicked
    {
        add
        {
            clicked += value;
        }

        remove
        {
            clicked -= value;
        }
    }

    public static void Main() { }
}

see Compiler Error CS0071

Stécy
  • 11,951
  • 16
  • 64
  • 89
  • 1
    +1 for the correct implementation, however that wasn't the question asked. People tend to get to the real meat at the very bottom of their question. Kind of like how you wanted to explain WHY you did what you did before telling your parents that you got detention. –  Jun 11 '10 at 19:59
  • 5
    +1 Maybe not the explicit answer to the question, but this just saved me probably a good hour in trying to figure out a work around. – David Faivre May 16 '12 at 19:09
1

This would not actually be an original thought by myself.

However, I thought I might respond to this:

"Off the top of my head I don't see any theoretical or practical reason why we could not have field-like explicitly implemented events. Nor do I see any reason why we particularly need to. This may have to remain one of the mysteries of the unknown." -Eric Lippert


In Chapter 23 of A Programmer's Introduction to C#, Second Edition, Eric Gunnerson wrote:

"[I]f another class also wanted to be called when the button was clicked, the += operator could be used, like this:

button.Click += new Button.ClickHandler(OtherMethodToCall);

Unfortunately, if the other class wasn't careful, it might do the following:

button.Click = new Button.ClickHandler(OtherMethodToCall);

This would be bad, as it would mean that our ButtonHandler would be unhooked and only the new method would be called."

...

"What is needed is some way of protecting the delegate field so that it is only accessed using += and -=."


He goes on over the next few pages to comment on including the add() and remove() methods to implement this behavior; being able to write to those methods directly and the consequence of storage allocation for unneeded delegate references.

I would add more, but I respect the author too much to do so without his permission. I recommend finding a copy of this book and would recommend anything by Eric Gunnerson in general (blog, etc...)

Anyway, I hope this is relevant to the topic and if so, hope it shines light on this "mystery of the unknown"? (I was reading this very chapter and searching Stack Overflow for insight into event handler logic considerations when creating custom collections from custom objects) - I only mention this because I claim no specific authority on this particular subject. I am merely a student in search of "enlightenment" myself :-)

EtherealMonkey
  • 216
  • 4
  • 13
  • I really dislike the use of the same identifier for an auto-event and its underlying delegate. IMHO, the use of the event name should have been permissible for adding or removing a subscription, setting a subscription list to null or comparing it against null, or for invoking an event, *with invocation syntax automatically including the null check*. Any other usage should have required using an identifier for the delegate. – supercat Oct 23 '13 at 20:03