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?
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?
You could make it empty or launch a NotImplementedException
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 usingICollection
is to make other objects think that they can do with your object whatever they can do with any other collection.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.
You might want to look into the ReadOnlyCollection. You could make a private innerclass and let it implement
ICollection
. Then Make a method that returnsReadOnlyCollection
by callingAsReadOnly
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.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:
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:
In this case, the member will not appear unless the instance is cast to the interface type. It is still public.
You can implement the interface explicitly and have the implementation hidden:
The user can't call
urClassInstance.Clear()
directly, but they can call((ICollection)urClassInstance).Clear()
indirectly like this.