“使用 MyNameSpace”和“使用 MyNameSpace”之间的区别和“命名空间MyNameSpace”
你好,我是 ASP.NET 的新手。我很困惑“using MyNameSpace;”之间有什么区别和“命名空间 MyNameSpace”。我的演示代码如下...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
使用MyNameSpace;
命名空间MyNameSpace
{
public partial class DemoPage : System.Web.UI.Page
{
My code here
}
}
在上面的代码中,两个突出显示的语句之间是否有任何区别。如果是的话那是什么?
提前致谢...
Hello I am new to asp.net. I am confused what is the difference between "using MyNameSpace;" and "namespace MyNameSpace". My demo code is as follow...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyNameSpace;
namespace MyNameSpace
{
public partial class DemoPage : System.Web.UI.Page
{
My code here
}
}
In the above code is there any difference between the two highlighted statements or not. If yes then what is it?
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,他们提供补充服务。
像这样的 using 指令:
告诉编译器在解析简单名称时在命名空间
MyNamespace
中查找 - 因此,如果您有一个名为MyNamespace.Foo
的类型,则只需使用 < code>Foo 在你的源代码中,当你得到这个 using 指令时引用它。然而,命名空间声明有效地表示,“我在这个块中声明的任何内容都在给定的命名空间中”。因此,要声明
MyNamespace.Foo
类型,您可以使用:看到了吗?
using
指令表示您想要使用特定命名空间中的事物,而命名空间声明是将事物放入特定命名空间中。Yes, they provide complementary services.
A using directive like this:
Tells the compiler to look in the namespace
MyNamespace
when resolving simple names - so if you have a type calledMyNamespace.Foo
, you can just useFoo
in your source to refer to it when you've got this using directive.However, the namespace declaration effectively says, "Anything I declare within this block is in the given namespace". So to declare the
MyNamespace.Foo
type, you'd use:Do you see? The
using
directive says that you want to use things in a particular namespace, whereas the namespace declaration is about putting things into a particular namespace.using
用于在该命名空间内创建类型名称的“快捷方式”。仅当您编写的代码位于另一个名称空间内时才需要这样做。namespace
用于定义命名空间:示例
在文件
first.cs
中:在文件
second.cs
中:在上面的代码示例中,
temp
变量的声明不需要包含命名空间,因为它在using
指令中提到。using
is used for creating a "shortcut" to typenames within that namespace. This is only needed when the code you write is within another namespace.namespace
is used for defining a namespace:Example
In file
first.cs
:In file
second.cs
:In the above code sample, the declaration of the
temp
variable does not need to include the namespace, since it is mentioned in ausing
directive.是的,有区别。
namespace
语句用于创建命名空间,而using
语句用于使编译器重新识别已经存在的命名空间。在您的代码中,
using
语句不起作用,因为您的所有代码都位于该名称空间中,因此它已经知道它。当您
使用 System.Web.UI
时,System.Web.UI.Page
标识符可以直接写为Page
,正如编译器所知关于该命名空间中的类。如果没有using
语句,则需要编译器的完全限定名称才能知道在哪里可以找到该类。Yes, there is a difference. The
namespace
statement is used to create a namespace, while theusing
statement is used to make the compiler regognise an already existing namespace.In your code the
using
statement has no effect, as all your code is in that namespace so it already knows about it.As you have
using System.Web.UI
, theSystem.Web.UI.Page
identifier could be written as justPage
as the compiler knows about the classes in that namespace. If you wouldn't have thatusing
statement, you would need the fully qualified name for the compiler to know where to find the class.