Does “using” statement always dispose the object?

后端 未结 3 886
渐次进展
渐次进展 2020-12-16 10:04

Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.:

using (var myClassInsta         


        
3条回答
  •  囚心锁ツ
    2020-12-16 10:45

    If the object implements IDisposable, it will be called.

    From using Statement (C# Reference) by MSDN

    Defines a scope, outside of which an object or objects will be disposed.

    The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

提交回复
热议问题