如何避免每个 .cs 文件中出现大量使用运算符?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
我必须将上述代码放入几乎每个 .cs 文件中。有什么办法可以避免吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,它们告诉编译器您的代码正在引用哪些名称空间。它们声明了一个范围并且编译需要它们。
有关 .NET 框架中的命名空间,请参阅 Microsoft 的以下官方文档:
http://msdn.microsoft.com/en-us/library/dfb3cx8s%28VS.80%29.aspx
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
将一个类添加到您的项目中并键入这些 using 语句。文件+导出模板。选择“项目模板”、“下一步”。勾选你的班级,下一步。勾选 System 和 System.Core,下一步。在这里根据您的口味选择好的值。结束。
现在,您可以使用“项目 + 添加新项”从此模板启动新的源代码文件。
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.
如果您想使用短类名,则无法删除每个 .cs 文件顶部的 using 指令。避免使用 using 指令的唯一解决方案是在各处使用完全限定的类名。
如果这太过激进,我建议您将 using 指令包含在一个区域中,并在 IDE 允许的情况下折叠它。
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.
好吧,您可以使用事物的完全限定名称。因此,您可以不只是使用
var x = new StringBuilder();
,而是使用var x = new System.Text.StringBuilder();
。对System.Text
命名空间中的所有内容执行此操作,您就不再需要 System.Text using 指令。大多数时候,使用 using 指令会更好,但有时您会遇到一种情况,一次只需要命名空间中的一种类型,然后您也可以只使用全名。此外,您可能会发现其中一些尚未使用。例如,您的大多数类可能不直接使用 System.IO 中的类型。程序倾向于仅封装一两个类,而其他类型将使用这些类而不是核心 System.IO 类型。
Well, you could use fully qualified names for things. So instead of just
var x = new StringBuilder();
you could sayvar x = new System.Text.StringBuilder();
. Do that for everything in theSystem.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.
检查是否可以将这些语句包含在 IDE 的新文件模板中。
Check if you can include those statements in new file template in your IDE.
使用 Resharper。
(这实际上是很多问题的答案,而不仅仅是这个问题。)每当您键入项目中引用的程序集中的名称而不是项目中声明的名称空间时,Resharper 都会自动提示您并将名称空间声明插入到您的代码中。代码。它还会提醒您注意冗余的命名空间声明,以便您可以轻松删除代码中未使用的命名空间声明。
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.