Should I Dispose() DataSet and DataTable?

前端 未结 12 1501
清酒与你
清酒与你 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:48

    Do you create the DataTables yourself? Because iterating through the children of any Object (as in DataSet.Tables) is usually not needed, as it's the job of the Parent to dispose all its child members.

    Generally, the rule is: If you created it and it implements IDisposable, Dispose it. If you did NOT create it, then do NOT dispose it, that's the job of the parent object. But each object may have special rules, check the Documentation.

    For .NET 3.5, it explicitly says "Dispose it when not using anymore", so that's what I would do.

提交回复
热议问题