When are C# “using” statements most useful?

后端 未结 14 1913
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 05:33

So a using statement automatically calls the dispose method on the object that is being \"used\", when the using block is exited, right?

But when is this necessary/benef

14条回答
  •  迷失自我
    2021-02-13 05:52

    The 'using' statement is most useful when working with unmanaged objects, like database connections.

    In this way, the connection is closed and disposed no matter what happens in the code block.

    For more discussion, see this article on CodeProject: http://www.codeproject.com/KB/cs/tinguusingstatement.aspx

提交回复
热议问题