When are C# “using” statements most useful?

后端 未结 14 2190
没有蜡笔的小新
没有蜡笔的小新 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

    They are useful in any situation when you want cleanup code to be called on an object deterministically and regardless of exceptions (since a using statement is really just a try/finally).

    Most often they are used to clean up unmanaged resources but you can use them however you see fit.

提交回复
热议问题