var 关键字而不使用“using someNamespace”

发布于 2024-08-31 21:12:48 字数 615 浏览 5 评论 0原文

即使您没有在顶部包含必要的 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 usings at all?!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一桥轻雨一伞开 2024-09-07 21:12:48

您使用构造函数的示例将不起作用,但稍微复杂一点的情况会起作用。例如,假设您有三种类型:

  • 命名空间 N1 中的 class Foo
  • 命名空间 N2 中的
  • class Bar 命名空间 N3 中的 class Baz

现在假设 Bar 有一个返回 Foo 实例的方法:

public static Foo GetFoo() { ... }

这里,Bar.cs 需要 N1 的 using 指令,除非它指定了完整的名称。

现在假设我们在 Baz 中有这样的代码:

using N2;
...
var foo = Bar.GetFoo();

它将编译,但

using N2;
...
Foo foo = Bar.GetFoo();

不会。原因是 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:

  • class Foo in namespace N1
  • class Bar in namespace N2
  • class Baz in namespace N3

Now suppose Bar has a method which returns an instance of Foo:

public static Foo GetFoo() { ... }

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:

using N2;
...
var foo = Bar.GetFoo();

That will compile, but

using N2;
...
Foo foo = Bar.GetFoo();

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 return N1.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.

日裸衫吸 2024-09-07 21:12:48

简短的回答是不会。

您所看到的行为和差异肯定还有其他原因。

您能否用一个简短但完整的程序重现该问题,以便将其发布到此处?

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.

岁月打碎记忆 2024-09-07 21:12:48

它知道对象的完全限定类型,因为该类型位于引用的程序集中之一。

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 say MyType instead of SomeNamespace.MyType.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文