views:

154

answers:

6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

I have to put the above code in almost every .cs file. Is there any way to avoid it?

+10  A: 

No, those tell the compiler which namespaces your code is referencing. They declare a scope and they're needed for compilation.

See the following official documentation from Microsoft regarding namespaces in the .NET framework:

http://msdn.microsoft.com/en-us/library/dfb3cx8s%28VS.80%29.aspx

The Matt
+1  A: 

Check if you can include those statements in new file template in your IDE.

el.pescado
Yup - just automate it and then don't worry about it! A tool like Resharper will make it even more brain-dead simpler.
Terry Donaghe
Is it possible in Visual Studio?
Sergey
+2  A: 

Well, you could use fully qualified names for things. So instead of just var x = new StringBuilder(); you could say var x = new System.Text.StringBuilder();. Do that for everything in the System.Text namespace and you remove the need for a System.Text using directive. Most of the time you're better off with the using directive, but now and then you'll have a situation where you only need one type from namespace one time, and then you may as well just use the full name.

Also, you might find some of those are already un-used. For example, most of your classes probably don't use types from System.IO directly. Programs tend to encapsulate IO work just a class or two, and your other types will use those rather than core System.IO types.

Joel Coehoorn
+4  A: 

Add a class to your project and type these using statements. File + Export Template. Select Item template, Next. Tick your class, Next. Tick System and System.Core, Next. Pick good values here, to your taste. Finish.

You can now start a new source code file from this template with Project + Add New Item.

Hans Passant
+4  A: 

If you want to use short class names, you cannot get rid of the using directives at the top of each .cs file. The only solution to avoid the using directive is to used fully qualified class names everywhere.

If this is too radical, I suggest you to enclose the using directive into a region, and to collapse it if you IDE allows it.

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
#endregion
Laurent Etiemble
+1  A: 

Use Resharper.

(That is actually the answer to a lot of questions, not just this one.) Resharper automatically prompts you and inserts namespace declarations into your code whenever you type a name that is in an assembly referenced in your project but not a namespace declared in your code. It also alerts you to redundant namespace declarations so you can easily trim out the ones your code isn't using.

Robert Rossney
+1: Indeed. With ReSharper, it's as easy as hitting 'ALT+TAB' to add the appropriate 'using statements' (provided that you've added the appropriate assembly references).
Jim G.
Well actually Resharper goes further than that (if the assembly is already referenced VS we do that for you) Resharper just has to know an assembly with a potential match and It'll ask if you wish to reference that assembly and put the using directive in place
Rune FS
I thought that might be the case, but I felt uncomfortable ascribing magical powers to it.
Robert Rossney