0

I am currently trying to override the Remove function of the generic List-Class. But I am struggling with one tiny part of my approach - with the reference to an object outside of the Remove-method.

public new void Remove(ref string item)
{
    if (Count > 9)
    {
        Remove(this[0]);
    }
    base.Remove(item);
}

This method doesnt work because it is not overriding the actual Remove-method.

Does anyone know how to handle this?

EDIT: in the remove function I want to call a method on the reference object.

EDIT2: my current version

class SocketList<WebSocketConnection>
{
    private List<WebSocketConnection> theList = new List<WebSocketConnection>();
    public void Remove(ref WebSocketConnection obj)
    {
        obj.Dispose();
        theList.Remove(obj);
        // additional stuff
    }
}

But in this version it is not possible to call the Dispose method on the referenced object. Im getting a message that says that there is no such method available for this object.

EDIT3: This is the Class in which I want to call the Dispose method

public class WebSocketConnection : IWebSocketConnection
{
    {...}
    // Flag: Has Dispose already been called?
    private bool disposed = false;
    // Instantiate a SafeHandle instance.
    private SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
    {...}
    // Public implementation of Dispose pattern callable by consumers.
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    // Protected implementation of Dispose pattern.
    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
            return;
        if (disposing)
        {
            handle.Dispose();
            // Free any other managed objects here.
            //
        }
        // Free any unmanaged objects here.
        //
        disposed = true;
    }
}
Snickbrack
  • 1,253
  • 4
  • 21
  • 56
  • What you want is just plain impossible. What's your reason for wanting this? Is it just curiosity, or is there a bigger problem you're trying to solve? That bigger problem may get more useful answers. –  Jun 02 '16 at 07:11
  • Do you really want a `ref string item`-signature? What type of elements does your list contain? Are there only strings in it or any complex objects? – MakePeaceGreatAgain Jun 02 '16 at 07:23
  • Have you considered implementing IList instead? – user6144226 Jun 02 '16 at 07:34
  • 1
    As I undestand, you are trying to writte a limited queue. This answer could feet to your needs :) http://stackoverflow.com/questions/5852863/fixed-size-queue-which-automatically-dequeues-old-values-upon-new-enques This should be a cleaner way to achieve this :) Or you can writte an extension method. There is many way to do that :) I posted that as an answer, apparently people wanted me to put that as a comment. – pix Jun 02 '16 at 07:35

1 Answers1

4

You can´t override any member of List<T> because non of them is virtual. Also new won´t solve this because it simply hides the base-implementation. You may your list as MyExtList<MyClass> a = new MyExtList<MyClass>(). However whenever you cast this instance to its base-class also the base-implementation is used instead of your "overridden" one.

However you can do the following to achieve what you really want:

class MyExtList<T>
{
    private List<T> theList = new List<T>();

    public void Remove(T obj) 
    {
        theList.Remove(obj)
        // additional stuff
    }
}

Thuse you won´t even need inheritance of List<T> but use a compoisition which gives you much more flexibility on what you offer to the user of your class. With your current implementation the user could do everything with the list what he could do with the generic List-implementation also. However in most situation you´re just interested in providing a few methods instead of all. So you can nicely hide everything you won´t provide to the outside by making a completely new class that uses List<T> instead of deriving from it.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • Hi, thanks for your answer, thats primarly what I wanted to archive. But like you can see I need to call a Dispose-Method on the referenced object. But with your solution his is not possible. How should I change the code to archive this? – Snickbrack Jun 02 '16 at 09:56
  • Sure it is, simply call `obj.Dispose` within the method. Having it removed from a list does not affect your instances state in any way so you can easily access and dispose it. – MakePeaceGreatAgain Jun 02 '16 at 10:00
  • I tried that but getting an error message saying that there is no Dispose function available. But there is one in this class. I have updated the code in the question... – Snickbrack Jun 02 '16 at 10:04
  • You should add the code of the class you want to put into the list - at least the dispose-method and the class´ signature. – MakePeaceGreatAgain Jun 02 '16 at 10:08
  • I can´t see any reason for this error to appear as from the code you´ve posted the method *does* exist on your class. Anyway you should consider to create a new question as your updates are far away from the original pupose of the question, aren´t they? – MakePeaceGreatAgain Jun 02 '16 at 10:21