How to hide some members of an interface

2019-01-19 16:41发布

I would like to create a custom collection that implements ICollection.

But I would like not to expose some memebers of ICollection like Clear method.

How to achieve this?

7条回答
太酷不给撩
2楼-- · 2019-01-19 17:02

You could make it empty or launch a NotImplementedException

查看更多
迷人小祖宗
3楼-- · 2019-01-19 17:11

What makes you so sure that you really need to implement ICollection? If you don't want some methods from this interface, don't use it, declare a new interface with methods that you want. The whole point of using ICollection is to make other objects think that they can do with your object whatever they can do with any other collection.

查看更多
何必那么认真
4楼-- · 2019-01-19 17:12

I would rather suggest you to consider "Composition" over "Inheritance" here.

That gives you more control over what all to expose to the outer world, with added advantage of dynamic binding with the actual collection.

查看更多
女痞
5楼-- · 2019-01-19 17:13

You might want to look into the ReadOnlyCollection. You could make a private innerclass and let it implement ICollection. Then Make a method that returns ReadOnlyCollection by calling AsReadOnly on that object. Or just subclass it if that fits your design. It is preferred to subclass this collection rather than try to create your own implementation.

查看更多
够拽才男人
6楼-- · 2019-01-19 17:15

You can't. Interface members are always public... otherwise, the class would fail to implement the interface. That's why access modifiers are not allowed in interface member declarations.

There are two ways of declaring members that satisfy the interface requirements: implicitly and explicitly.

Implicitly, any public member with a matching signature will be used:

public interface IGuess
{
    void Guess();
}

public class Guy : IGuess
{
    public void Guess() {}
}

This is a "normal" member of the class and will be reflected on instances of the type.

You can also, as @Jaroslav points out, explicitly designate members as satisfying an interface definition:

public class Guy : IGuess
{
    void IGuess.Guess() {}
}

In this case, the member will not appear unless the instance is cast to the interface type. It is still public.

查看更多
smile是对你的礼貌
7楼-- · 2019-01-19 17:21

You can implement the interface explicitly and have the implementation hidden:

public class UrClass : ICollection
{
    void ICollection.Clear() { ... }
}

The user can't call urClassInstance.Clear() directly, but they can call ((ICollection)urClassInstance).Clear() indirectly like this.

查看更多
登录 后发表回答