Using the “clear” method vs. New Object

前端 未结 5 1966
有刺的猬
有刺的猬 2020-12-05 17:13

In the .NET framework, many of the System.Collection classes have Clear methods on them. Is there a clear advantage on using this versus replacing the reference

5条回答
  •  星月不相逢
    2020-12-05 18:08

    replacing the reference will not release the collection right away it will need to wait for garbage collector to dispose the object,

    if you want to reutilize the same object collection then use .Clear() if not you will have the two objects in memory for some time.

提交回复
热议问题