Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear a dictionary of dictionaries

Tags:

c#

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

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

What's the best practice regarding clearing myDict? Do I have to loop on all the values and call Clear on each or is it enough if I just called Clear on myDict??
I'm looking for the best performance too.

like image 774
Rami Avatar asked Mar 09 '23 08:03

Rami


1 Answers

If you call Clear(), it will clear the master dictionary. If there are no references any more to the inner dictionaries, the garbage collector will clean them up, so there is no need to call Clear() on them. If they are still being referenced, you have to unreference those first on you might end up with an invalid state.

like image 79
Patrick Hofman Avatar answered Mar 20 '23 02:03

Patrick Hofman