What is the relationship between the using keyword and the IDisposable interface?

后端 未结 7 522
栀梦
栀梦 2020-12-10 18:38

If I am using the using keyword, do I still have to implement IDisposable?

7条回答
  •  北海茫月
    2020-12-10 19:03

    You are confusing things. You can only use the "using" keyword on something that implements IDisposable.

    Edit: If you use the using keyword, you don't have to explicity invoke Dispose, it will be called automatically at the end of the using block. Others have already posted examples of how the using statement is translated into a try - finally statement, with Dispose invoked within the finally block.

提交回复
热议问题