Differences between Dictionary.Clear and new Dictionary()

后端 未结 8 830
情书的邮戳
情书的邮戳 2020-12-15 03:33

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

相关标签:
8条回答
  • 2020-12-15 04:01

    I suppose the key difference is that if you call Dictionary.Clear(), all references to that dictionary will be cleared. If you use new Dictionary(), then the reference you are dealing with at the time will be cleared (in a sense), but all other places you have references won't be since they will still be referring to the old dictionary.

    Hopefully this code illustrates it simply:

    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 had called d1.Clear() instead, then d1 and d2 would have remained in sync, and the subsequent adds would have added to both. The WriteLine calls would both have output '3' instead.

    0 讨论(0)
  • 2020-12-15 04:04

    Dictionary.Clear will remove (but not dispose) all items in the instance whereas new Dictionary() will create a brand new instance which just happens to be empty. There may be some subtle implications between the two. 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 all of the contents from the original collection will be printed. That is because the enumerator was created from the reference variable before it was reassigned to a new instance.

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

    0 讨论(0)
  • 2020-12-15 04:06

    If you have several references to that dictionary and you clear it you will affect them all where as if you create a new dictionary you will only affect the one new reference. I guess it depends on the scope of the impact that you're aiming for. Obviously a call to "new" will give you a "cleared" dictionary but you might lose other useful state information.

    0 讨论(0)
  • 2020-12-15 04:08

    Dictionary.clear will evict all of the items in your dictionary. new Dictionary creates a new dictionary object in memory while orphaning your previous Dictionary for the garbage collector to clean up later.

    0 讨论(0)
  • 2020-12-15 04:09

    Dictionary.Clear()
    This will remove all key/value pairs within the Dictionary. The Garbage Collector will clear the memory for these items during the next Garbage Collection Cycle. MSDN

    new Dictionary()
    Creates a new Dictionary object in memory and abandons the original object. The memory would be cleared during the next Garbage Collection Cycle.

    0 讨论(0)
  • 2020-12-15 04:20

    As has been extensively covered, the effects are essentially the same. Clear() and new would both give you a fresh Dictionary, essentially abandoning the references to the objects inside of it, freeing them to be collected if they became unrooted.

    Technically, .Clear() would have an advantage over new if you were about to repopulate the dictionary to the same size as it was before. This is because it would have been already resized internally to hold the number of objects that you had in it before. Creating a new Dictionary would have a new internal hashtable with the default size, and it would have to be re-expanded as you added items to it.

    I would also suggest that they communicate a completely different intent, and you should use .Clear() in your code when you're dealing with the same context as before. Creating a new Dictionary implies that you are about to embark on some new logic dealing with something different than the old Dictionary, while using Clear() indicates that yes, you really wanted to just reset everything you've done so far with it.

    0 讨论(0)
提交回复
热议问题