2

Possible Duplicates:
Why should you remove unnecessary C# using directives?
overhead to unused “using” declarations ?

I was wondering, is it matter if I use extra using statements, does it take more time to compile, does it cause anything bad? Thanks.

Community
  • 1
  • 1
Rich Porter
  • 205
  • 4
  • 14

2 Answers2

7

using statements should be used when you are working with objects that implement the IDisposable interface such as streams, GDI objects, database connections, etc... This ensures that those objects will be properly disposed of and eventually free any associated resources with them.


UPDATE:

I might have misunderstood your question about using. If you are referring to bringing namespaces into scope then no, there is nothing bad if you leave unused using statements. The compiler will automatically remove them and they will not be part of the generated IL. They will neither slow your compile time nor do anything bad. Personally I don't leave them as they are ugly to my eyes, but its from pure aesthetics and personal point of view. Visual Studio has a nice refactor setting when you right click in the source code which allows you to reorgnanize and remove unused using statements.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

No. It's simply syntactic sugar for try/finally Dispose.

IDisposable myDisposable;

try
{
    myDisposable = new MyDisposable();
}
finally 
{
    myDisposable.Dispose();
}
Jeff
  • 35,755
  • 15
  • 108
  • 220