I am creating a list derived from CollectionBase, so that I can use my own methods. This is made in a dll project. I just wanted to know if it is possible to hide the default methods from CollectionBase, so that only my methods appear when this list is being used.
Create a custom list without default methods
49 views Asked by user2858325 At
2
There are 2 answers
0
On
If you derive from CollectionBase, then no, it's not possible. Your class then is a CollectionBase and so inherits all its methods. The need to hide something that you inherit is a clear hint that you should not use inheritence.
You could use composition instead:
public class MyList
{
private List<int> _privateList = new List<int>();
public void MyMethod1()
{
// Do something
}
public void MyMethod2()
{
// Do something
}
public void Add(int x)
{
_privateList.Add(x);
}
// etc.
}
If its virtual method, you can override and change to whatever behavior you want. If its not a virtual method, you can't hide them.
In any case you can re-declare the method with
new
(with same signature) and mark it withObsolete
attribute. This will force that compiler generates error if someone wants to use them. In following example callingChild.Method()
will generate compiler error. This guarantees that no one can use the parent's method.