如何通过保留方法名来扩展接口?

给出两个接口:

interface I1 { int Foo(); } interface I2 { void Foo(); } 

一堂课:

 class Test : I1, I2 { int I1.Foo() { Console.WriteLine("I1.Foo"); return default(int); } public void Foo() { Console.WriteLine("I2.Foo"); } } 

如何通过保持名为Foo的方法将接口I2扩展为I1

我尝试了以下代码,但它没有编译:

 interface I1 { int Foo(); } interface I2 : I1 { void I2.Foo(); } class Test : I2 { /* same code */ } 

在示例中不清楚在接口本身中明确声明I2.Foo()如果允许则会完成。 规范(s.13.4.1)允许实现接口的结构或类声明显式成员实现。 (接口不能声明任何实现,显式或其他)。

因此,假设我们已经定义:

 interface IFoo { void Bar(); } interface IBaz : IFoo { new void Bar(); } interface IQux : IBaz { } class A : IQux // equivalent to class A : IQux, IBaz, IFoo (spec sec. 13.4.6) { void IFoo.Bar() { Console.WriteLine("IFoo.Bar"); } void IBaz.Bar() { Console.WriteLine("IBaz.Bar"); } public void Bar() { Console.WriteLine("A.Bar"); } // Not allowed: void IQux.Bar() {...} // Since "The fully-qualified name of the interface member // must reference the interface in which the member // was declared" (s. 13.4.1) } 

然后,以下驱动程序显示显式接口方法实现的效果。

 public static void Main() { A a = new A(); a.Bar(); // prints A.Bar (a as IFoo).Bar(); // prints IFoo.Bar (a as IBaz).Bar(); // prints IBaz.Bar (a as IQux).Bar(); // prints IBaz.Bar } 

不太确定你想要它做什么,但你可以这样做: –

 public interface I1 { int Foo(); } public interface I2:I1 { new void Foo(); } 

仅当两种方法具有不同的签名时才有效,这意味着它们必须具有不同数量的参数或不同类型的参数或两者。

为什么不给你的两个方法GetFooDoFoo

这会奏效

 public interface I1 { int Foo(); } public interface I2 : I1 { void Foo(int i); } 

这也行得通

 public interface I1 { int GetFoo(); } public interface I2 : I1 { void DoFoo(); } 

您也可以声明一个属性。 属性包括两个方法:getter和setter。

 public interface I { int Foo { get; set; } } public class C : I { private int _foo; public int Foo { get { // getter return _foo; } set { // setter _foo = value; } } }