Should I Dispose() DataSet and DataTable?

前端 未结 12 1564
清酒与你
清酒与你 2020-11-22 02:45

DataSet and DataTable both implement IDisposable, so, by conventional best practices, I should call their Dispose() methods.

However, from what I\'ve read so far, Da

12条回答
  •  情书的邮戳
    2020-11-22 02:55

    Here are a couple of discussions explaining why Dispose is not necessary for a DataSet.

    To Dispose or Not to Dispose ?:

    The Dispose method in DataSet exists ONLY because of side effect of inheritance-- in other words, it doesn't actually do anything useful in the finalization.

    Should Dispose be called on DataTable and DataSet objects? includes some explanation from an MVP:

    The system.data namespace (ADONET) does not contain unmanaged resources. Therefore there is no need to dispose any of those as long as you have not added yourself something special to it.

    Understanding the Dispose method and datasets? has a with comment from authority Scott Allen:

    In pratice we rarely Dispose a DataSet because it offers little benefit"

    So, the consensus there is that there is currently no good reason to call Dispose on a DataSet.

提交回复
热议问题