You may receive a CS0117 compiler error when you explicitly implement an interface event (821175)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# 2005, Express Edition

SYMPTOMS

You can implement interface events in a class explicitly. However, when you implement interface events explicitly, and you instantiate the class instance, you may receive the following compiler error message:
error CS0117: 'namespace. class name' does not contain a definition for Interface name'

CAUSE

When a class explicitly implements events of an interface, explicitly implemented events are not accessible from class instances.

WORKAROUND

To work around this problem, create an instance of the interface, and then access the events.

Use the following code to create an instance of the interface to access events:
IMyInterface eventSample = new ExplicitEventSample();
eventSample.MyEvent += new MyDelegate(FindLength);

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Paste the following code in Notepad:
    using System;
    
    namespace EventTest
    {
        public delegate int MyDelegate(string s);
    
        public interface IMyInterface
        {
            event MyDelegate MyEvent;
        }
    
        public class ExplicitEventSample: IMyInterface
        {
            event MyDelegate IMyInterface.MyEvent //Explicit implementation of IMyInterface.MyEvent
            {
                add
                {
                    MyEvent2Storage += value;
                }
                remove
                {
                    MyEvent2Storage -= value;
                }
            }
    
            public MyDelegate MyEvent2Storage; // underlying storage for IMyInterface.MyEvent.
    
        }
    
        class Class1
        {
            [STAThread]
            static int FindLength(string strArg)
            {
                return strArg.Length;
            }
            
            static void Main(string[] args)
            {
               ExplicitEventSample eventSample = new ExplicitEventSample();
    
               eventSample.MyEvent += new MyDelegate(FindLength);
               
           }
        }
    }
  2. Save the file as c:\Sample.cs.
  3. At the Visual Studio .NET command prompt, run the following command to compile the code:
    csc 	c:\Sample.cs
You may receive the compilation error that is mentioned in the "Symptoms" section of this article.

REFERENCES

For more information about explicit interface implementation, see the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:1/13/2006
Keywords:kbProgramming kbCompiler kbprb KB821175 kbAudDeveloper