Differences between Dictionary.Clear and new Dictionary()

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

    I believe that .Clear() was provided so that if your Dictionary is exposed as a read-only property you would be able to remove all of the items. However, if it's not exposed that way, and you have complete control then it might be easier to just instantiate a new Dictionary. There might be a slight performance difference between the two, also.

提交回复
热议问题