c# - Override Lists Remove function failed -
i trying override remove function of generic list-class. struggling 1 tiny part of approach - reference object outside of remove-method.
public new void remove(ref string item) { if (count > 9) { remove(this[0]); } base.remove(item); }
this method doesnt work because not overriding actual remove-method.
does know how handle this?
edit: in remove function want call method on reference object.
edit2: 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 version not possible call dispose method on referenced object. im getting message says there no such method available object.
edit3: class in want call dispose method
public class websocketconnection : iwebsocketconnection { {...} // flag: has dispose been called? private bool disposed = false; // instantiate safehandle instance. private safehandle handle = new safefilehandle(intptr.zero, true); {...} // public implementation of dispose pattern callable 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 other managed objects here. // } // free unmanaged objects here. // disposed = true; } }
you can´t override member of list<t>
because non of them virtual
. new
won´t solve because hides base-implementation. may list myextlist<myclass> = new myextlist<myclass>()
. whenever cast instance base-class base-implementation used instead of "overridden" one.
however can following achieve want:
class myextlist<t> { private list<t> thelist = new list<t>(); public void remove(t obj) { thelist.remove(obj) // additional stuff } }
thuse won´t need inheritance of list<t>
use compoisition gives more flexibility on offer user of class. current implementation user list generic list-implementation also. in situation you´re interested in providing few methods instead of all. can nicely hide won´t provide outside making new class uses list<t>
instead of deriving it.