Reputation: 3563
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?
Upvotes: 3
Views: 856
Reputation: 9407
Interfaces are meant to be exposed publicly.
What you can do though is switch to an abstract base class with protected abstract members. In many ways this will achieve the same end by forcing implementers to have the method without it being public.
public abstract class MyBase
{
protected abstract void DoSomething();
}
The main con to this is that you don't get multiple inheritance with abstract classes like you do with interfaces, so weigh how important this need is to you.
Upvotes: 4
Reputation: 152521
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();
Upvotes: 6
Reputation: 3724
Short answer: No.
The basic idea of an interface is that its a contract between your classes and components, so that means its members are intended for public use. If you need your members private that probably means you might want to revisit your design.
Upvotes: 4