var 关键字而不使用“using someNamespace”
即使您没有在顶部包含必要的 using
声明,Visual Studio/intellisense 如何知道如何处理声明为 var
的变量?
例如,我在不同的命名空间中定义了类 MyDomainObject
如果我不在文件中声明 using TheOtherNameSpace;
,以下代码将无法编译:
private void Foo()
{
MyDomainObject myObj = new MyDomainObject();
// Doesn't know what this class is
}
但是如果我使用 var
var myObj = new MyDomainObject();
这将编译,并且智能感知确切地知道我可以做什么与它。
那么,在没有 using
的情况下,它到底是如何知道类型是什么的呢?
(顺便说一句,如果它不需要 using
就知道,为什么我们还需要 using
?!)
How does Visual Studio/intellisense know what to do with a variable declared as var
even if you don't include the necessary using
declaration at the top?
For example, I have class MyDomainObject
defined in a different namespace
If I don't declare using TheOtherNameSpace;
in the file the following code won't compile:
private void Foo()
{
MyDomainObject myObj = new MyDomainObject();
// Doesn't know what this class is
}
But if I use var
var myObj = new MyDomainObject();
This will compile, and intellisense knows exactly what I can with it.
So how the heck does it know what the type is without the using
?
(And as an aside, if it knows without the using
, why do we need using
s at all?!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用构造函数的示例将不起作用,但稍微复杂一点的情况会起作用。例如,假设您有三种类型:
现在假设
Bar
有一个返回Foo
实例的方法:这里,Bar.cs 需要 N1 的 using 指令,除非它指定了完整的名称。
现在假设我们在 Baz 中有这样的代码:
它将编译,但
不会。原因是 using 指令的存在只是为了让编译器知道名称“Foo”的含义 - 它的完全限定名称是什么。在第一个代码段中,
Bar.GetFoo()
被有效地声明为返回N1.Foo
,因此编译器没有问题。在第二个片段中,编译器首先看到“Foo”,但对 N1 一无所知,因此不知道如何查找它。Your example with a constructor won't work, but a slightly more involved situation will. For example, suppose you have three types:
Now suppose
Bar
has a method which returns an instance ofFoo
:Here, Bar.cs would need a using directive for N1, unless it specified the name in full.
Now suppose that we have this code in Baz:
That will compile, but
won't. The reason is that using directives are only there so that the compiler knows what the name "Foo" means - what its fully qualified name is. In the first snippet,
Bar.GetFoo()
is effectively declared to returnN1.Foo
, so the compiler is fine. In the second snippet, the compiler first sees "Foo" and doesn't know anything about N1, so doesn't know how to look it up.简短的回答是不会。
您所看到的行为和差异肯定还有其他原因。
您能否用一个简短但完整的程序重现该问题,以便将其发布到此处?
var
关键字没有什么神奇之处,它只是根据赋值右侧的表达式(在本例中)推断所需的类型,因此没有理由为什么其中一个代码各个部分应该比其他部分工作得更好。Short answer is that it doesn't.
There must be some other reason for the behavior and difference you're seeing.
Can you reproduce the problem with a short, but complete, program so that you could post it here?
There is nothing magical about the
var
keyword, it simply infers the type needed based on the expression on the right hand side of the assignment (in this case), so there is no reason why one of the code pieces should work better than the other.它知道对象的完全限定类型,因为该类型位于引用的程序集中之一。
using SomeNamespace
只是一种简写,可以说MyType
而不是SomeNamespace.MyType
。It knows the fully-qualified type of the object because this type is in one of the referenced assemblies.
using SomeNamespace
is only a shorthand making it possible to sayMyType
instead ofSomeNamespace.MyType
.