Differences between Dictionary.Clear and the new dictionary ()

What are the main differences between Dictionary.Clear and new Dictionary() in C #? Which one is recommended for which cases?

+27
dictionary c #
Sep 09 '09 at 15:53
source share
8 answers

Dictionary.Clear() will remove all KeyValue pairs in the dictionary. Running new Dictionary() will create a new dictionary instance.

If and only if the old version of the dictionary is not connected to another link, creating a new dictionary will make the entire dictionary, and the content (which is not embedded in another place) is available for cleaning the GC.

Dictionary.Clear() will make KeyValue pairs available for cleaning.

In practice, both options will have very similar effects. The difference will be what happens when it is used in a method:

 void NewDictionary(Dictionary<string,int> dict) { dict = new Dictionary<string,int>(); // Just changes the local reference } void ClearDictionary(Dictionary<string,int> dict) { dict.Clear(); } // When you use this... Dictionary<string,int> myDictionary = ...; // Set up and fill dictionary NewDictionary(myDictionary); // myDictionary is unchanged here, since we made a new copy, but didn't change the original instance ClearDictionary(myDictionary); // myDictionary is now empty 
+32
Sep 09 '09 at 16:04
source share

As has been widely encompassed, the effects are essentially the same. Clear() and new will both give you a fresh Dictionary, essentially abandoning references to objects inside it, freeing them for collection if they are disabled.

Technically, .Clear() will take precedence over new if you intend to re-populate the dictionary to the same size as before. This is due to the fact that it has already been changed internally in order to keep the number of objects that you had in it before. Creating a new dictionary will have a new internal hash table with a default size, and it will need to be re-expanded as items are added to it.

I also suggest that they exchange completely different intentions, and you should use .Clear() in your code when dealing with the same context as before. Creating a new dictionary implies that you are going to start some kind of new logic regarding something other than the old dictionary, and using Clear() means yes, you really wanted to just reset everything that you have done so far with him.

+19
Sep 09 '09 at 16:13
source share

Dictionary.Clear ()
This will remove all key / value pairs in the dictionary. The garbage collector will clear the memory of these items during the next garbage collection cycle. MSDN

new dictionary ()
Creates a new Dictionary object in memory and leaves the original object. The memory will be cleared during the next garbage collection cycle.

+14
Sep 09 '09 at 15:57
source share

I believe the key difference is that if you call Dictionary.Clear() , all references to this dictionary will be cleared. If you use new Dictionary() , then the link you are currently working with will be cleared (in a sense), but in all other places where you have links, it will not, since they will still link to old dictionary.

Hope this code just illustrates this:

 public void Method() { Dictionary<int, int> d1 = new Dictionary(); d1.Add(1,1); d1.Add(2,3); Dictionary<int, int> d2 = d1; //this line only 'clears' d1 d1 = new Dictionary(); d1.Add(1,1); d1.Add(3,5); d2.Add(2,2); //writes '2' followed by '1' Console.WriteLine(d1.Count); Console.WriteLine(d2.Count); } 

If I called d1.Clear() instead, then d1 and d2 would remain in sync, and subsequent additions would add to both. For WriteLine calls, both will have an output of "3".

+5
09 Sep '09 at 16:06
source share

Dictionary.Clear will delete (but not delete) all the elements in the instance, while new Dictionary() will create a completely new instance that just turns out to be empty. There may be some subtle consequences between them. Consider the following examples.

 void Example1() { var collection = new Dictionary<string, string>(); collection.Add("key1", "value1"); collection.Add("key2", "value2"); foreach (var kvp in collection) { collection = new Dictionary<string, string>(); Console.WriteLine(kvp); } } void Example2() { var collection = new Dictionary<string, string>(); collection.Add("key1", "value1"); collection.Add("key2", "value2"); foreach (var kvp in collection) { collection.Clear(); Console.WriteLine(kvp); } } 

In the first example, the entire contents of the source collection will be printed. This is because the enumerator was created from a reference variable before it was reassigned to a new instance.

In the second example, the collection is cleared during enumeration, which will result in an InvalidOperationException because the collection has been modified.

+5
Sep 09 '09 at 16:11
source share

I believe .Clear () was provided so that if your dictionary was open as a read-only property, you could remove all the elements. However, if it was not detected this way, and you have full control, it would be easier to just create a new dictionary. There may also be a slight difference in performance between the two.

+2
Sep 09 '09 at 16:41
source share

If you have several references to this dictionary, and you clear it, you will affect them all, where, as if you created a new dictionary, you will affect only one new link. I think it depends on the scale of the impact you are striving for. Obviously, calling the β€œnew” will give you a β€œcleared” dictionary, but you may lose other useful status information.

+1
Sep 09 '09 at 16:06
source share

Dictionary.clear will supersede all elements of your dictionary. the new dictionary creates a new dictionary object in memory, and the orphan of the previous Dictionary for the garbage collector to clear later.

0
Sep 09 '09 at 15:55
source share



All Articles