Default using directives in new C# files

2019-01-18 03:29发布

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?

3条回答
叼着烟拽天下
2楼-- · 2019-01-18 03:44

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-18 03:45

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.

查看更多
迷人小祖宗
4楼-- · 2019-01-18 04:06

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 :)

查看更多
登录 后发表回答