Calling Dispose() vs when an object goes out scope/method finishes

前端 未结 6 738
情深已故
情深已故 2020-12-30 05:58

I have a method, which has a try/catch/finaly block inside. Within the try block, I declare SqlDataReader as follows:

SqlDataReader         


        
6条回答
  •  醉话见心
    2020-12-30 06:04

    I am puzzled by the statement "In the finally block, the objects which are manually disposed of are those which are set at the class level." By objects set at the class level, do you mean fields? You probably shouldn't be disposing of these within a ordinary method, because then the life-time of the fields is unpredictable, and depends on which methods you happened to have called. It would be better to implement IDisposable and dispose of fields in your Dispose method.

提交回复
热议问题