visual-studio 新 C# 文件中的默认 using 指令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/651055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:17:21  来源:igfitidea点击:

Default using directives in new C# files

visual-studiodefaultusing-directives

提问by compie

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

为什么 Visual Studio 2008 会自动将以下using 指令插入到我创建的每个新 C# 文件中?

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

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

这些命名空间有什么特别之处?这些是最常用的吗?

回答by Jon Skeet

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.

是的,它们经常使用,仅此而已,所以 MS 将它们放在Visual Studio 模板中。我个人经常使用“排序和删除未使用的使用”,因此它们经常消失。

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

编辑:如果您成为“使用指令排序和删除未使用的”的粉丝,您应该掌握Visual StudioPowerCommands- 它添加了一个解决方案资源管理器上下文菜单项来为整个项目执行此操作,而不仅仅是一个文件:)

回答by Andrew Flanagan

If you like, you can change them. See herefor 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.csfile within that's used to generate all new C# source files - it looks like this:

如果打开%Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip,则可以修改其中class.cs用于生成所有新 C# 源文件的文件 - 如下所示:

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

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

You can then add or remove the usingdirectives 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 /setupto refresh Visual Studio's template cache. Now all new C# files you create should match your modified template.

然后,您可以在此文件的顶部添加或删除所需的using指令,并将其保存回存档。最后运行%Program Files%\Microsoft Visual Studio 8\Common7\IDE\devenv.exe /setup以刷新 Visual Studio 的模板缓存。现在,您创建的所有新 C# 文件都应与修改后的模板匹配。

回答by Guffa

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.

这是在该特定类型的项目中被选为新文件模板中的命名空间。不同类型的项目有不同的模板,因此有不同的 using 指令集。using 指令的选择取决于该类型文件的需要以及您可能使用的内容。

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

using 指令只告诉编译器在哪里寻找类,所以使用代码不需要的 using 指令没有坏处,只要它们不会引起任何冲突(类名不明确)。

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

如果您右键单击文件并打开Organise Usings子菜单,您会找到Remove Unused Usings可用于删除文件中不需要的 using 指令的选项。