Differences between Dictionary.Clear and new Dictionary()

后端 未结 8 846
情书的邮戳
情书的邮戳 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: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.

提交回复
热议问题