When to use using [duplicate]
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.
精彩评论