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?
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 theclass.cs
file within that's used to generate all new C# source files - it looks like this: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.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 optionRemove Unused Usings
that you can use to remove using directives that it not needed in the file.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 :)