How to hide some members of an interface

前端 未结 7 2222
甜味超标
甜味超标 2020-12-03 11:42

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

But I would like not to expose some memebers of ICollection like

7条回答
  •  难免孤独
    2020-12-03 12:34

    If you just want to hide those members from your own collection's interface, you can define them explicitly.

    void ICollection.Clear() {
        // ...
    }
    

    Explicitly defined members are only available if the instance is used through that interface.

    YourCollection col1 = new YourCollection();
    col1.Clear(); // this is not allowed if clear is defined explicitly
    
    ICollection col2 = new YourCollection();
    col2.Clear(); // this will work because col2 is ICollection
    

提交回复
热议问题