Clear Dictionary Dictionary

I have a private field in the class, which is a dictionary of dictionaries.

private readonly Dictionary<string, Dictionary<int, int>> 
myDict = new Dictionary<string, Dictionary<int, int>>();

What is the best cleaning practice myDict? Do I need to quote all the values ​​and call Clear for each, or is it enough if I just called Clear on myDict?
I am also looking for better performance.

+4
source share
3 answers

If you call Clear(), it will clear the master dictionary. If there are no more links in the internal dictionaries, the garbage collector will clear them, so there is no need to call Clear()them. If they are still referenced, you must first abandon them, you may be in an invalid state.

+7
source

Clear on myList, .

, myList , , .

0

If you keep (pointer-) copies of 2nd level dictionaries somewhere, this is a splash screen to clear them all. If you access them directly through the mother dictionary or temporary variables inside the method that they are collected by the garbage collector sooner or later, so you do not need Clearthem separately.

0
source

Source: https://habr.com/ru/post/1675024/


All Articles