0

Possible Duplicate:
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

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 4
    Duplicate of [When should I use "using" blocks in C#?](http://stackoverflow.com/questions/567138/when-should-i-use-using-blocks-in-c) – James McNellis Nov 08 '10 at 04:46

2 Answers2

2

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.

EMP
  • 59,148
  • 53
  • 164
  • 220
  • Just to explicitly note the special cases, in some cases the using block forces you to write some not so excellent code (despite the object's implementation of IDisposable), so give it a moment's thought before opening those braces. – Neowizard Nov 08 '10 at 04:53
1

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.

zerkms
  • 249,484
  • 69
  • 436
  • 539