When are C# “using” statements most useful?

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

    All the answers about deterministic vs. undeterministic and how the using works under the hood are correct.

    But to pick your specific sample, don't forget that almost all System.Drawing (GDI+ wrapper) objects also hold references to unmanaged memory, so that you may run into trouble when using those extensively without disposing them properly (where using is the easiest way).

提交回复
热议问题