开发者

When to use using [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplic开发者_C百科ate:

When should I use “using” blocks in C#?

A lot of classes maybe need to be wrapped by using. Is there a simple rule for the using?

Rick


If a class implements IDisposable then generally you should wrap its use in using (if you can). In my experience that's not "a lot of classes". It should only be classes that use unmanaged resources and classes that wrap those.


If class implements IDisposable - you should Dispose() objects after you've finished working with them.

Since using() {} is a syntactic sugar for this task - you should wrap all IDisposable classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜