Sergey
Sergey

Reputation: 49758

How to avoid many using operators in every .cs file?

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?

Upvotes: 0

Views: 309

Answers (6)

Robert Rossney
Robert Rossney

Reputation: 96750

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.

Upvotes: 1

Laurent Etiemble
Laurent Etiemble

Reputation: 27889

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

Upvotes: 4

Hans Passant
Hans Passant

Reputation: 941665

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.

Upvotes: 4

Joel Coehoorn
Joel Coehoorn

Reputation: 415901

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.

Upvotes: 3

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

Upvotes: 1

The Matt
The Matt

Reputation: 6636

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

Upvotes: 10

Related Questions