[英]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.