Suppose I have a Collection of some kind that itself contains Collections; e.g., Dictionary(Of String, List(Of MyClass)). If I want to clear the Collection entirely, does it make any sense to clear every individual child collection before clearing the parent collection, like so:
For Each ListDef As KeyValuePair(Of String, List(Of MyClass)) In MasterDictionary
Dim ThisList As List(Of MyClass) = ListDef.Value
ThisList.Clear()
Next
MasterDictionary.Clear()
Or does this really accomplish nothing more than simply:
MasterDictionary.Clear()
I'm asking if there is any reason whatsoever -- performance, safety, clarity, etc. -- to use the first method. I generally use the second method myself, as I'm assuming that it implicitly achieves what the first method does anyway. But we all know how dangerous it is to assume sometimes; hence this question.
EDIT: Today, in my own application, I have seen compelling evidence that the first method might be preferable in certain cases. In my application I have some global collections (Dictionaries containing Lists, as in the example code above) that are populated when the user clicks a button labelled "Load Data". When the user clicks "Unload Data" to start back at the beginning, formerly I was clearing these collections using the second method above.
The problem was that when the user clicked "Load Data" a second time, the application would suddenly become very slow. Eventually it would finish populating the collections again, but at a snail's pace. I couldn't figure out how to fix this problem until I finally tried the first method above. Now reloading and repopulating the collections works just as quickly as loading them the first time.
Based on the answers posted so far, it sounds like I must have "other code somewhere" referencing the child collections; however, it doesn't look to me like I do. The only places I reference these children are where I iterate over their parents; if the parents are cleared, then they should have no children that the code is aware of, correct?
Hopefully someone can help me to understand how either these references are still lingering around, or else what is going on that I am overlooking.