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

前端 未结 6 728
情深已故
情深已故 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:20

    No, objects are not automatically disposed when they go out of scope.

    They're not even guaranteed to be disposed if/when they're garbage-collected, although many IDisposable objects implement a "fallback" finaliser to help ensure that they're eventually disposed.

    You are resposible for ensuring that any IDisposable objects are disposed, preferably by wrapping them in a using block.

提交回复
热议问题