Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If in my program I have the interface, then all it's members are public implicitly. And in the class, which implements that interface, I must make that members (properties) public too.

Is it any way to make it private?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
249 views
Welcome To Ask or Share your Answers For Others

1 Answer

Is it any way to make an interface implementation private?

Not completely private - an interface represents a public set of methods and properties. There is no way to make interface implementations private.

What you can do is make the implementation explicit:

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.")
    }

}

Now the only way to call IFoo.Bar() is explicitly through the interface:

FooImpl f = new FooImpl();
f.Bar();   // compiler error
((IFoo)f).Bar();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...