20

Why does Visual Studio 2008 automatically insert the following using directives into each new C# file I create?

using System; 
using System.Collections.Generic; 
using System.Text;

What's so special about these namespaces? Are these the most frequently used ones?

compie
  • 10,135
  • 15
  • 54
  • 78
  • PowerCommands for Visual Studio 2010 http://visualstudiogallery.msdn.microsoft.com/e5f41ad9-4edc-4912-bca3-91147db95b99/ – raminjacobson Aug 08 '12 at 20:18

3 Answers3

36

Yes, they're frequently used, that's all, so MS put them in the Visual Studio templates. Personally I use "sort and remove unused usings" pretty frequently, so they often go away.

If you want to remove them, you can amend the "new class" template.

EDIT: If you become a fan of "Sort and Remove Unused Using Directives" you should get hold of PowerCommands for Visual Studio - that adds a Solution Explorer context menu item to do it for a whole project instead of just one file :)

Stuart Whitehouse
  • 1,421
  • 18
  • 30
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hey Jon, that [amend the "new class" template](http://dotnet.org.za/hiltong/archive/2008/02/26/amending-the-new-class-visual-studio-c-template.aspx) link seems to have died. Was it [along these lines](http://blogs.msdn.com/b/steve/archive/2007/04/10/changing-the-default-using-directives-in-visual-studio.aspx) or have things improved? (Seems downright barbaric to have to edit a zip file just to change the default imports.) – T.J. Crowder Dec 12 '12 at 18:27
  • @T.J.Crowder: I'm afraid I really can't remember what the page looked like 3 1/2 years ago... but it sounds like it was probably roughly the same, yes. – Jon Skeet Dec 12 '12 at 18:30
  • Great answer Jon, as usual. I prefer [CodeMaid](http://www.codemaid.net/) over PowerCommands though! – pim May 10 '18 at 15:14
4

If you like, you can change them. See here for more info.

--- Below is the main part of article in the case the link ceases. ---

If you open %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip, you can modify the class.cs file within that's used to generate all new C# source files - it looks like this:

using System;
using System.Collections.Generic;
using System.Text;

namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

You can then add or remove the using directives you want at the top of this file, and save it back to the archive. Finally run %Program Files%\Microsoft Visual Studio 8\Common7\IDE\devenv.exe /setup to refresh Visual Studio's template cache. Now all new C# files you create should match your modified template.

Andrew Flanagan
  • 4,267
  • 3
  • 25
  • 37
2

That's the namespaces that was selected to be in the template for a new file, in that specific type of project. Different types of projects have different templates and thus different sets of using directives. The using directives were just chosen depending on what's needed for that type of file, and what you are likely to use.

The using directive only tells the compiler where to look for classes, so there is no harm in having using directives that is not neccesarily needed by the code, as long as they don't cause any conflicts (ambiguous class names).

If you right click in the file and open the Organise Usings submenu, you find the option Remove Unused Usings that you can use to remove using directives that it not needed in the file.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Well, there's harm in terms of readability I'd say. I think it's a good idea to keep the list of using directives tidy - hence the goodness of Remove Unused Usings (and the Sort and Remove version too). – Jon Skeet Mar 16 '09 at 16:14