[英]Strange limitation in the implementation of internal interface
我有代码
internal interface IFoo
{
void foo();
}
public class A : IFoo
{
// error CS0737: 'A' does not implement interface member 'IFoo.foo()'.
//'A.foo()' cannot implement an interface member because it is not public.
internal void foo()
{
Console.WriteLine("A");
}
}
为何如此奇怪的限制? 我有内部接口,为什么我不能在接口实现中创建内部方法?
这是因为接口无法指定有关成员可见性的任何内容,只能指定成员本身。 实现接口的所有成员必须是public
。 实现private
接口时也会发生同样的情况。
一种解决方案可能是明确实现接口:
internal interface IFoo
{
void foo();
}
public class A : IFoo
{
void IFoo.foo()
{
Console.WriteLine("A");
}
}
在上面的代码,你必须有一个实例A
投地IFoo
能够调用foo()
但你只能做这样的投如果你是internal
比类,因此访问IFoo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.