Question

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.

Was it helpful?

Solution 2

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 with Obsolete attribute. This will force that compiler generates error if someone wants to use them. In following example calling Child.Method() will generate compiler error. This guarantees that no one can use the parent's method.

public class Parent
{
    public void Method()
    {
    }
}

public class Child : Parent
{
    [Obsolete("This method should not be used", true)]
    public new void Method()
    {
    }
}

OTHER TIPS

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.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top