Why call Dispose()? Memory leak won't occur?

前端 未结 7 700
无人及你
无人及你 2020-12-29 10:01

Edit: My question isn\'t getting the main answer that I was looking for. I wasn\'t clear. I would really like to know two things:

  1. Can NOT call
相关标签:
7条回答
  • 2020-12-29 10:53

    For me:

    Dispose can be used in using() scope. This can help me to determine the lifespan of a IDisposeable component. I usually use this in StreamWriter/Reader or SqlConnection classes.

    Another use of Dispose is it can end a lifespan of a component explicitly. Such as calling Form.Dispose() in C# winform will close the form. However, for SqlConnection, people said that just calling Dispose alone without explicitly calling Close won't guarantee the connection to be closed. Advised to call both Close and Dispose. I haven't tried this though.

    And another thing, after Dispose() is called, the GC can immediaetly free the memory, because they know that the object lifespan is end, rather than waiting for the lifespan to end.

    Similiar question may be C# disposing IDisposable

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