Compiler Error CS0686

访问符“accessor”无法实现类型“type”的接口成员“member”。使用显式接口实现。

提示:当实现一个接口时,如果其中包含的方法名称与属性或事件关联的自动生成的方法相冲突,会出现此错误。属性的 get/set 方法生成为 get_property 和 set_property,事件的 add/remove 方法生成为 add_event 和 remove_event。如果接口包含其中的任何一个方法,则发生冲突。要避免此错误,请使用显式接口实现来实现这些方法。要这样做,请将函数指定为:


      Interface.get_property() { /* */ }
Interface.set_property() { /* */ }

以下示例生成 CS0686:

// CS0686.cs
interface I
{
    int get_P();
}

class C : I
{
    public int P
    {
        get { return 1; }  // CS0686
    }
}
// But the following is valid:
class D : I
{
    int I.get_P() { return 1; }
    public static void Main() {}
}

声明事件时也会发生此错误。事件结构自动生成 addevent 和 removeevent 方法,这可能和接口中的同名方法相冲突,如下示例所示:

// CS0686b.cs
using System;

interface I
{
    void add_OnMyEvent(EventHandler e);
}

class C : I
{
    public event EventHandler OnMyEvent
    {
        add { }  // CS0686
        remove { }
    }
}

// Correct (using explicit interface implementation):
class D : I
{
    void I.add_OnMyEvent(EventHandler e) {}
    public static void Main() {}
}