界面的私有成员

Dim*_*zyr 3 c#

如果在我的程序中我有接口,那么它的所有成员都是隐式公开的.在实现该接口的类中,我必须公开该成员(属性).

是否可以将其设为私有

D S*_*ley 5

是否可以将接口实现设为私有?

完全私有 - 接口表示一组公共方法和属性.没有办法让接口实现私有化.

可以做的是明确实现:

public interface IFoo
{
   void Bar();
}

public class FooImpl
{
    void IFoo.Bar()
    {
       Console.WriteLine("I am somewhat private.")
    }

    private void Bar()
    {
       Console.WriteLine("I am private.")
    }

}
Run Code Online (Sandbox Code Playgroud)

现在唯一的方法IFoo.Bar()是通过接口显式调用:

FooImpl f = new FooImpl();
f.Bar();   // compiler error
((IFoo)f).Bar();
Run Code Online (Sandbox Code Playgroud)