0
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?

The Matt
  • 6,618
  • 7
  • 44
  • 61
Sergey
  • 47,222
  • 25
  • 87
  • 129
  • 7
    yeah, don't write C# – Muad'Dib Dec 28 '09 at 17:23
  • 4
    Do you really want to go back to the days of C++ header files with numerous levels of 'inclusion' and indirection and try to figure out exactly what is accessible or not within a program module? C# using statements make it very clear to both the compiler and programmer exactly what is accessible. – LBushkin Dec 28 '09 at 17:27
  • Just curious - Can this be done in the app.config or web.config using the "Add Assembly" configuration element? – David Dec 28 '09 at 17:29
  • @David Stratton - Yes, but the references in the web.config are global I believe. – The Matt Dec 28 '09 at 17:30
  • 2
    -1: Why do you want to do this? Why do the 'using statements' represent clutter to you? – Jim G. Dec 28 '09 at 18:05
  • LBushkin: false dichotomy. There are languages which include (the equivalent of) System, Collections.Generic, Linq, etc., automatically. Come to think of it, why would someone ever *not* want these included? You could write your own class called "Array", but that seems like a bad idea. It might make one of my source files (which doesn't use all of these) compile immeasurably faster, but the stdlib is already all in cache from the other files I wrote that do use it. Is there some hidden benefit to forcing me to write these all the time that I'm just not seeing? – Ken Dec 28 '09 at 18:37
  • @Ken: If I'm not dealing with Collections? Or LINQ? Or not doing IO? – Powerlord Dec 28 '09 at 18:41
  • @Ken there might be very good reasons why you'd what to name class the same as some of the classes in the above namespaces. If you were to write an application for a publishing house you might end up with a "Dictionary" domain object (I.e. not a collection but a book) but even more importantly you might want to have your own extension methods for say select, where and other LINQ working on the same types as the standard linq extensions. including those from System.Linq would be a bad idea then. always preferre the explicit to the implict :) – Rune FS Dec 28 '09 at 21:52
  • Bemrose: OK, you've named a case, but what's the *benefit* in that case? It builds 0.01s faster? You want to write a class called "StreamReader" in your (non-IO-doing) program? What about System -- when's the last time you wrote a program that didn't use System? Still not seeing it... – Ken Dec 28 '09 at 23:40

6 Answers6

10

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
  • 6,618
  • 7
  • 44
  • 61
4

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
  • 922,412
  • 146
  • 1,693
  • 2,536
4

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
  • 27,111
  • 5
  • 56
  • 81
3

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
  • 399,467
  • 113
  • 570
  • 794
1

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

1

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
  • 94,622
  • 24
  • 146
  • 218
  • +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. Dec 28 '09 at 18:11
  • 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 Dec 28 '09 at 21:56
  • I thought that might be the case, but I felt uncomfortable ascribing magical powers to it. – Robert Rossney Dec 28 '09 at 21:58