Quote:Original post by Arild Fines
Quote:Original post by kSquared
Quote:Original post by GDKnight
I could iterate through the nodes contained in it and set them to null but will this delete it? Im afraid it might set a reference to nothing leaving the data intact in memory.
One way is to implement the Dispose pattern.
Eh, why? Are you going to require that list elements implement IDisposable as well? Otherwise there's no point.
He didn't say what it was a list
of. If this is unmanaged resources we're talking about, it's much better to code defensively than to wait for it to come back and bite you. Clearly it's not POD, since he's trying to set them to null -- you can't set a value type to null. That said, adding a trial check like "Node toDispose = _node as IDisposable;" wouldn't be unreasonable.
Quote:There's no iteration needed. As long as all node objects are unreachable from a GC root, they will be collected.
That statement is a little misleading -- you forgot to add the word "eventually" to the end. Remember, when an object is collected could be a very long time after when it is no longer f-reachable; do you really want your linked list of FileStreams holding on forever? Probably not.
In addition, your solution of merely setting the head and tail to null is not one I'm very fond of. An external reference to one of the nodes would still appear to be valid to the GC (and would never get disposed), even though the rest of the list is supposedly "cleared". This is an inconsistent object state and suggests poor design.
You correctly clarify this by saying that it works only if you don't hand out references externally, but what good is a collection class where you can't see the elements? The "linked list" described with that code looks more like a double-ended stack (you can see the top and the bottom but not anything in between).