Visual Basic 自动导入命名空间 [英] Visual basic auto imports namespaces
问题描述
在 C# 中,列出了一些默认名称空间,例如 System.Collections,而无需使用 blah 键入.在visual basic中,它们不是为您导入的.有没有办法强制 vb 自动导入某些默认名称空间或 VB 的工作方式与 C# 不同?
In C# some of default name space such as System.Collections are listed without typing in using blah. In visual basic, they are not imports for you. Is there a way to force vb to auto imports some of default name space or VB work differently than C#?
推荐答案
我认为 John Rudy 发布的第一项就是您要查找的内容 - 将它们添加到项目属性中.
I think the first item posted by John Rudy is what you're looking for- add them in the project properties.
但是,VB.Net 的工作方式也与 C# 不同,因为在 VB 中导入命名空间与在 C# 中的含义不同.当您在 VB 中导入命名空间时,从某种意义上说,它还带来了范围内"的子命名空间.
However, VB.Net does also work differently than C#, in that it means a different thing in VB to import a namespace than it does in C#. When you import a namespace in VB, it also brings child namespaces 'in scope', in a manner of speaking.
以 System
命名空间为例,它是默认导入的.因为 System 命名空间是导入的,所以您不必像在 C# 中那样首先键入 System.
来引用像 IO
这样的子命名空间.所以,开箱即用,您可以在 VB 中说这样的话:
Take the System
namespace, for example, which is imported by default. Because the System namespace is imported, you don't have to first type System.
to reference a child namespace like IO
, like you would in C#. So, right out of the box you can say something like this in VB:
If IO.File.Exists(MyFile) Then ....
现在在 C# 中这是不可能的.您还必须导入 System.IO
,然后只需说 File.Exists()
或列出 System 命名空间:System.IO.File.存在()
.
That just isn't possible in C# right now. You either have to also import System.IO
and then just say File.Exists()
or list out the System namespace as well: System.IO.File.Exists()
.
它可能看起来不是很重要,但一段时间后您真的会习惯这个 VB 功能,而且它比您想象的更方便.我提出所有这些是因为此功能的最终结果是您通常不想在 VB 中导入与在 C# 中一样多的命名空间.
It may not seem very significant, but you really get used to this VB feature after a while, and it comes in handy more than you'd think. I bring all this up because the end result of this feature is that you often don't want to import as many namespaces in VB as you do in C#.
这篇关于Visual Basic 自动导入命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!