Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide some members of an interface

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?

like image 675
nan Avatar asked Mar 12 '11 19:03

nan


1 Answers

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.

like image 90
Jaroslav Jandek Avatar answered Sep 30 '22 06:09

Jaroslav Jandek