[Mono-list] Strange explicit implementation

Jonathan Pryor jonpryor@vt.edu
03 Dec 2002 00:19:25 -0500


You are correct: the syntax provided in the MSDN example is
incorrect--it shouldn't have `()' after the event name, it should look
like a property declaration.  (Like, not identical to -- it uses
add/remove instead of get/set).

Despite what it looks like, an event declaration *does not* declare a
data member.  At least not necessarily.  It's closer in spirit to a
property declaration, declaring a pair of functions that implementing
classes must provide.  The compiler conveniently provides default
implementations of these functions for you, if you don't choose to
implement them yourself.

The general idea of explicit implementation of events is to have the
same behavior as explicit implementation of properties/methods: limit
access to the member only to instances of that class.  For example:

        // explicit implementation of events

        using System;

        delegate void Handler ();

        interface IFoo {
          event Handler Bar;
          void Invoke ();
        }

        interface IBar {
          // note: name collides with IFoo.Bar
          event Handler Bar;
          void Invoke ();
        }

        class X : IFoo, IBar {
          private event Handler _foo_bar;
          private event Handler _bar_bar;

          //
          // IFoo implementation
          //
          event Handler IFoo.Bar {
            add {
              Console.WriteLine ("X.IFoo.Bar:add invoked");
              _foo_bar += value;
            }
            remove {
              Console.WriteLine ("X.IFoo.Bar:remove invoked");
              _foo_bar -= value;
            }
          }

          void IFoo.Invoke () {
            _foo_bar ();
          }

          //
          // IBar implementation
          //
          event Handler IBar.Bar {
            add {
              Console.WriteLine ("X.IBar.Bar:add invoked");
              _bar_bar += value;
            }
            remove {
              Console.WriteLine ("X.IBar.Bar:remove invoked");
              _bar_bar -= value;
            }
          }

          void IBar.Invoke () {
            _bar_bar ();
          }

        }

        class Run {
          public static void Main () {
            X x = new X ();
            // Causes compiler error CS0117: `X' does not contain a 
            //    definition for `Bar'
            // x.Bar += new Handler (CB);
            IFoo f = (IFoo) x;
            f.Bar += new Handler (CB);
            f.Invoke();
          }

          private static void CB () {
            Console.WriteLine ("Callback function");
          }
        }

As for why you'd want to do this...  For the same reason that any
interface member can be explicitly implemented -- to provide a way to
disambiguate same-named members from different interfaces, as
demonstrated above.

 - Jon

On Mon, 2002-12-02 at 14:20, Jaime Anguiano Olarra wrote:
> Hi all,
> 
> I am looking at MS.NET docs and I have found an strange explicit
> interface implementation:
> 
> interface IFoo {
> 	event Handler Bar;
> 	...
> }
> 
> class X : IFoo {
> 	event Handler IFoo.Bar() 
> 	{
> 	}
> 	...
> }
> 
> For me that's a strange declaration. It's not legal anyway since
> interface explicit implementations for events must be property syntaxed
> but I really can't understand what that code can mean. I would like to
> know what's that supposed to work.
> 
> Anyway, it's not legal so don't worry too much, but if it's written must
> be because you might think of writing it, no?.
> 
> Thanks,
> 
> Jaime.